Skip to content

Commit 56b2711

Browse files
committed
Auto merge of rust-lang#101464 - JohnTitor:rollup-unsjgm6, r=JohnTitor
Rollup of 7 pull requests Successful merges: - rust-lang#99291 (Add let else drop order tests) - rust-lang#101402 (Add a Machine hook for inline assembly) - rust-lang#101404 (Fix cleanup for uninitialized stdout) - rust-lang#101418 (Revert "Mention rust-analyzer maintainers when `proc_macro` bridge is changed") - rust-lang#101425 (Point at type parameter in plain path expr) - rust-lang#101426 (Don't duplicate file descriptors into stdio fds) - rust-lang#101447 (Remove generics_def_id_map from the resolver.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 676afc5 + 0d8a1f4 commit 56b2711

File tree

14 files changed

+433
-82
lines changed

14 files changed

+433
-82
lines changed

compiler/rustc_ast_lowering/src/item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8585
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
8686
allow_gen_future: Some([sym::gen_future][..].into()),
8787
allow_into_future: Some([sym::into_future][..].into()),
88+
generics_def_id_map: Default::default(),
8889
};
8990
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
9091

compiler/rustc_ast_lowering/src/lib.rs

+38-45
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ struct LoweringContext<'a, 'hir> {
132132
allow_try_trait: Option<Lrc<[Symbol]>>,
133133
allow_gen_future: Option<Lrc<[Symbol]>>,
134134
allow_into_future: Option<Lrc<[Symbol]>>,
135+
136+
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
137+
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
138+
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
139+
/// field from the original parameter 'a to the new parameter 'a1.
140+
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
135141
}
136142

137143
trait ResolverAstLoweringExt {
@@ -142,12 +148,6 @@ trait ResolverAstLoweringExt {
142148
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
143149
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
144150
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
145-
/// Record the map from `from` local def id to `to` local def id, on `generics_def_id_map`
146-
/// field.
147-
fn record_def_id_remap(&mut self, from: LocalDefId, to: LocalDefId);
148-
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
149-
/// `generics_def_id_map` field.
150-
fn get_remapped_def_id(&self, local_def_id: LocalDefId) -> LocalDefId;
151151
}
152152

153153
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -215,41 +215,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
215215
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
216216
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
217217
}
218-
219-
/// Push a remapping into the top-most map.
220-
/// Panics if no map has been pushed.
221-
/// Remapping is used when creating lowering `-> impl Trait` return
222-
/// types to create the resulting opaque type.
223-
#[instrument(level = "debug", skip(self))]
224-
fn record_def_id_remap(&mut self, from: LocalDefId, to: LocalDefId) {
225-
self.generics_def_id_map.last_mut().expect("no map pushed").insert(from, to);
226-
}
227-
228-
fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId {
229-
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
230-
// push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
231-
//
232-
// Consider:
233-
//
234-
// `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
235-
//
236-
// We would end with a generics_def_id_map like:
237-
//
238-
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
239-
//
240-
// for the opaque type generated on `impl Sized + 'b`, We want the result to be:
241-
// impl_sized#'b, so iterating forward is the wrong thing to do.
242-
for map in self.generics_def_id_map.iter().rev() {
243-
if let Some(r) = map.get(&local_def_id) {
244-
debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");
245-
local_def_id = *r;
246-
} else {
247-
debug!("def_id_remapper: no remapping for `{local_def_id:?}` found in map");
248-
}
249-
}
250-
251-
local_def_id
252-
}
253218
}
254219

255220
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -522,13 +487,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
522487
self.resolver
523488
.node_id_to_def_id
524489
.get(&node)
525-
.map(|local_def_id| self.resolver.get_remapped_def_id(*local_def_id))
490+
.map(|local_def_id| self.get_remapped_def_id(*local_def_id))
526491
}
527492

528493
fn local_def_id(&self, node: NodeId) -> LocalDefId {
529494
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
530495
}
531496

497+
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
498+
/// `generics_def_id_map` field.
499+
fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId {
500+
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
501+
// push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
502+
//
503+
// Consider:
504+
//
505+
// `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
506+
//
507+
// We would end with a generics_def_id_map like:
508+
//
509+
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
510+
//
511+
// for the opaque type generated on `impl Sized + 'b`, We want the result to be:
512+
// impl_sized#'b, so iterating forward is the wrong thing to do.
513+
for map in self.generics_def_id_map.iter().rev() {
514+
if let Some(r) = map.get(&local_def_id) {
515+
debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");
516+
local_def_id = *r;
517+
} else {
518+
debug!("def_id_remapper: no remapping for `{local_def_id:?}` found in map");
519+
}
520+
}
521+
522+
local_def_id
523+
}
524+
532525
/// Freshen the `LoweringContext` and ready it to lower a nested item.
533526
/// The lowered item is registered into `self.children`.
534527
///
@@ -597,9 +590,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
597590
remap: FxHashMap<LocalDefId, LocalDefId>,
598591
f: impl FnOnce(&mut Self) -> R,
599592
) -> R {
600-
self.resolver.generics_def_id_map.push(remap);
593+
self.generics_def_id_map.push(remap);
601594
let res = f(self);
602-
self.resolver.generics_def_id_map.pop();
595+
self.generics_def_id_map.pop();
603596
res
604597
}
605598

@@ -2027,7 +2020,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20272020
let name = match res {
20282021
LifetimeRes::Param { param, .. } => {
20292022
let p_name = ParamName::Plain(ident);
2030-
let param = self.resolver.get_remapped_def_id(param);
2023+
let param = self.get_remapped_def_id(param);
20312024

20322025
hir::LifetimeName::Param(param, p_name)
20332026
}

compiler/rustc_const_eval/src/interpret/machine.rs

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::borrow::{Borrow, Cow};
66
use std::fmt::Debug;
77
use std::hash::Hash;
88

9+
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
910
use rustc_middle::mir;
1011
use rustc_middle::ty::{self, Ty, TyCtxt};
1112
use rustc_span::def_id::DefId;
@@ -323,6 +324,15 @@ pub trait Machine<'mir, 'tcx>: Sized {
323324
kind: Option<MemoryKind<Self::MemoryKind>>,
324325
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
325326

327+
fn eval_inline_asm(
328+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
329+
_template: &'tcx [InlineAsmTemplatePiece],
330+
_operands: &[mir::InlineAsmOperand<'tcx>],
331+
_options: InlineAsmOptions,
332+
) -> InterpResult<'tcx> {
333+
throw_unsup_format!("inline assembly is not supported")
334+
}
335+
326336
/// Hook for performing extra checks on a memory read access.
327337
///
328338
/// Takes read-only access to the allocation so we can keep all the memory read

compiler/rustc_const_eval/src/interpret/terminator.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22

3+
use rustc_ast::ast::InlineAsmOptions;
34
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
45
use rustc_middle::ty::Instance;
56
use rustc_middle::{
@@ -166,8 +167,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
166167
terminator.kind
167168
),
168169

169-
// Inline assembly can't be interpreted.
170-
InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
170+
InlineAsm { template, ref operands, options, destination, .. } => {
171+
M::eval_inline_asm(self, template, operands, options)?;
172+
if options.contains(InlineAsmOptions::NORETURN) {
173+
throw_ub_format!("returned from noreturn inline assembly");
174+
}
175+
self.go_to_block(
176+
destination
177+
.expect("InlineAsm terminators without noreturn must have a destination"),
178+
)
179+
}
171180
}
172181

173182
Ok(())

compiler/rustc_middle/src/ty/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ pub struct ResolverAstLowering {
178178
pub label_res_map: NodeMap<ast::NodeId>,
179179
/// Resolutions for lifetimes.
180180
pub lifetimes_res_map: NodeMap<LifetimeRes>,
181-
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
182-
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
183-
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
184-
/// field from the original parameter 'a to the new parameter 'a1.
185-
pub generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
186181
/// Lifetime parameters that lowering will have to introduce.
187182
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, LifetimeRes)>>,
188183

compiler/rustc_resolve/src/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,6 @@ pub struct Resolver<'a> {
911911
label_res_map: NodeMap<NodeId>,
912912
/// Resolutions for lifetimes.
913913
lifetimes_res_map: NodeMap<LifetimeRes>,
914-
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
915-
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
916-
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
917-
/// field from the original parameter 'a to the new parameter 'a1.
918-
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
919914
/// Lifetime parameters that lowering will have to introduce.
920915
extra_lifetime_params_map: NodeMap<Vec<(Ident, NodeId, LifetimeRes)>>,
921916

@@ -1278,7 +1273,6 @@ impl<'a> Resolver<'a> {
12781273
import_res_map: Default::default(),
12791274
label_res_map: Default::default(),
12801275
lifetimes_res_map: Default::default(),
1281-
generics_def_id_map: Vec::new(),
12821276
extra_lifetime_params_map: Default::default(),
12831277
extern_crate_map: Default::default(),
12841278
reexport_map: FxHashMap::default(),
@@ -1445,7 +1439,6 @@ impl<'a> Resolver<'a> {
14451439
import_res_map: self.import_res_map,
14461440
label_res_map: self.label_res_map,
14471441
lifetimes_res_map: self.lifetimes_res_map,
1448-
generics_def_id_map: self.generics_def_id_map,
14491442
extra_lifetime_params_map: self.extra_lifetime_params_map,
14501443
next_node_id: self.next_node_id,
14511444
node_id_to_def_id: self.node_id_to_def_id,
@@ -1490,7 +1483,6 @@ impl<'a> Resolver<'a> {
14901483
import_res_map: self.import_res_map.clone(),
14911484
label_res_map: self.label_res_map.clone(),
14921485
lifetimes_res_map: self.lifetimes_res_map.clone(),
1493-
generics_def_id_map: self.generics_def_id_map.clone(),
14941486
extra_lifetime_params_map: self.extra_lifetime_params_map.clone(),
14951487
next_node_id: self.next_node_id.clone(),
14961488
node_id_to_def_id: self.node_id_to_def_id.clone(),

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1812,16 +1812,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18121812
return true;
18131813
}
18141814
}
1815-
// Notably, we only point to params that are local to the
1816-
// item we're checking, since those are the ones we are able
1817-
// to look in the final `hir::PathSegment` for. Everything else
1818-
// would require a deeper search into the `qpath` than I think
1819-
// is worthwhile.
1820-
if let Some(param_to_point_at) = param_to_point_at
1821-
&& self.point_at_path_if_possible(error, def_id, param_to_point_at, qpath)
1822-
{
1823-
return true;
1824-
}
1815+
}
1816+
// Notably, we only point to params that are local to the
1817+
// item we're checking, since those are the ones we are able
1818+
// to look in the final `hir::PathSegment` for. Everything else
1819+
// would require a deeper search into the `qpath` than I think
1820+
// is worthwhile.
1821+
if let Some(param_to_point_at) = param_to_point_at
1822+
&& self.point_at_path_if_possible(error, def_id, param_to_point_at, qpath)
1823+
{
1824+
return true;
18251825
}
18261826
}
18271827
hir::ExprKind::MethodCall(segment, receiver, args, ..) => {

library/std/src/io/stdio.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,24 @@ pub fn stdout() -> Stdout {
607607
}
608608
}
609609

610+
// Flush the data and disable buffering during shutdown
611+
// by replacing the line writer by one with zero
612+
// buffering capacity.
610613
pub fn cleanup() {
611-
// Flush the data and disable buffering during shutdown
612-
// by replacing the line writer by one with zero
613-
// buffering capacity.
614-
// We use try_lock() instead of lock(), because someone
615-
// might have leaked a StdoutLock, which would
616-
// otherwise cause a deadlock here.
617-
if let Some(lock) = STDOUT.get().and_then(ReentrantMutex::try_lock) {
618-
*lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
614+
let mut initialized = false;
615+
let stdout = STDOUT.get_or_init(|| {
616+
initialized = true;
617+
ReentrantMutex::new(RefCell::new(LineWriter::with_capacity(0, stdout_raw())))
618+
});
619+
620+
if !initialized {
621+
// The buffer was previously initialized, overwrite it here.
622+
// We use try_lock() instead of lock(), because someone
623+
// might have leaked a StdoutLock, which would
624+
// otherwise cause a deadlock here.
625+
if let Some(lock) = stdout.try_lock() {
626+
*lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
627+
}
619628
}
620629
}
621630

library/std/src/os/fd/owned.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl BorrowedFd<'_> {
104104
#[cfg(target_os = "espidf")]
105105
let cmd = libc::F_DUPFD;
106106

107-
let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 0) })?;
107+
// Avoid using file descriptors below 3 as they are used for stdio
108+
let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 3) })?;
108109
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
109110
}
110111

0 commit comments

Comments
 (0)