Skip to content

Commit 4d30011

Browse files
committed
Auto merge of #138532 - matthiaskrgr:rollup-mgcynqu, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #138283 (Enforce type of const param correctly in MIR typeck) - #138439 (feat: check ARG_MAX on Unix platforms) - #138502 (resolve: Avoid some unstable iteration) - #138514 (Remove fake borrows of refs that are converted into non-refs in `MakeByMoveBody`) - #138524 (Mark myself as unavailable for reviews temporarily) r? `@ghost` `@rustbot` modify labels: rollup
2 parents aa95b96 + 35aa49d commit 4d30011

21 files changed

+394
-49
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,22 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
17711771
{
17721772
span_mirbug!(self, constant, "bad static type {:?} ({:?})", constant, terr);
17731773
}
1774+
} else if let Const::Ty(_, ct) = constant.const_
1775+
&& let ty::ConstKind::Param(p) = ct.kind()
1776+
{
1777+
let body_def_id = self.universal_regions.defining_ty.def_id();
1778+
let const_param = tcx.generics_of(body_def_id).const_param(p, tcx);
1779+
self.ascribe_user_type(
1780+
constant.const_.ty(),
1781+
ty::UserType::new(ty::UserTypeKind::TypeOf(
1782+
const_param.def_id,
1783+
UserArgs {
1784+
args: self.universal_regions.defining_ty.args(),
1785+
user_self_ty: None,
1786+
},
1787+
)),
1788+
locations.span(self.body),
1789+
);
17741790
}
17751791

17761792
if let ty::FnDef(def_id, args) = *constant.const_.ty().kind() {

compiler/rustc_borrowck/src/universal_regions.rs

+14
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ impl<'tcx> DefiningTy<'tcx> {
183183
| DefiningTy::GlobalAsm(def_id) => def_id,
184184
}
185185
}
186+
187+
/// Returns the args of the `DefiningTy`. These are equivalent to the identity
188+
/// substs of the body, but replaced with region vids.
189+
pub(crate) fn args(&self) -> ty::GenericArgsRef<'tcx> {
190+
match *self {
191+
DefiningTy::Closure(_, args)
192+
| DefiningTy::Coroutine(_, args)
193+
| DefiningTy::CoroutineClosure(_, args)
194+
| DefiningTy::FnDef(_, args)
195+
| DefiningTy::Const(_, args)
196+
| DefiningTy::InlineConst(_, args) => args,
197+
DefiningTy::GlobalAsm(_) => ty::List::empty(),
198+
}
199+
}
186200
}
187201

188202
#[derive(Debug)]

compiler/rustc_codegen_ssa/src/back/command.rs

+41-6
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,42 @@ impl Command {
137137
/// Returns a `true` if we're pretty sure that this'll blow OS spawn limits,
138138
/// or `false` if we should attempt to spawn and see what the OS says.
139139
pub(crate) fn very_likely_to_exceed_some_spawn_limit(&self) -> bool {
140-
// We mostly only care about Windows in this method, on Unix the limits
141-
// can be gargantuan anyway so we're pretty unlikely to hit them
142-
if cfg!(unix) {
140+
#[cfg(not(any(windows, unix)))]
141+
{
143142
return false;
144143
}
145144

145+
// On Unix the limits can be gargantuan anyway so we're pretty
146+
// unlikely to hit them, but might still exceed it.
147+
// We consult ARG_MAX here to get an estimate.
148+
#[cfg(unix)]
149+
{
150+
let ptr_size = mem::size_of::<usize>();
151+
// arg + \0 + pointer
152+
let args_size = self.args.iter().fold(0usize, |acc, a| {
153+
let arg = a.as_encoded_bytes().len();
154+
let nul = 1;
155+
acc.saturating_add(arg).saturating_add(nul).saturating_add(ptr_size)
156+
});
157+
// key + `=` + value + \0 + pointer
158+
let envs_size = self.env.iter().fold(0usize, |acc, (k, v)| {
159+
let k = k.as_encoded_bytes().len();
160+
let eq = 1;
161+
let v = v.as_encoded_bytes().len();
162+
let nul = 1;
163+
acc.saturating_add(k)
164+
.saturating_add(eq)
165+
.saturating_add(v)
166+
.saturating_add(nul)
167+
.saturating_add(ptr_size)
168+
});
169+
let arg_max = match unsafe { libc::sysconf(libc::_SC_ARG_MAX) } {
170+
-1 => return false, // Go to OS anyway.
171+
max => max as usize,
172+
};
173+
return args_size.saturating_add(envs_size) > arg_max;
174+
}
175+
146176
// Ok so on Windows to spawn a process is 32,768 characters in its
147177
// command line [1]. Unfortunately we don't actually have access to that
148178
// as it's calculated just before spawning. Instead we perform a
@@ -165,9 +195,14 @@ impl Command {
165195
//
166196
// [1]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
167197
// [2]: https://devblogs.microsoft.com/oldnewthing/?p=41553
168-
169-
let estimated_command_line_len = self.args.iter().map(|a| a.len()).sum::<usize>();
170-
estimated_command_line_len > 1024 * 6
198+
#[cfg(windows)]
199+
{
200+
let estimated_command_line_len = self
201+
.args
202+
.iter()
203+
.fold(0usize, |acc, a| acc.saturating_add(a.as_encoded_bytes().len()));
204+
return estimated_command_line_len > 1024 * 6;
205+
}
171206
}
172207
}
173208

compiler/rustc_data_structures/src/unord.rs

+6
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ impl<V: Eq + Hash> UnordSet<V> {
259259
self.inner.is_empty()
260260
}
261261

262+
/// If the set has only one element, returns it, otherwise returns `None`.
263+
#[inline]
264+
pub fn get_only(&self) -> Option<&V> {
265+
if self.inner.len() == 1 { self.inner.iter().next() } else { None }
266+
}
267+
262268
#[inline]
263269
pub fn insert(&mut self, v: V) -> bool {
264270
self.inner.insert(v)

compiler/rustc_middle/src/ty/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3333
use rustc_data_structures::intern::Interned;
3434
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3535
use rustc_data_structures::steal::Steal;
36+
use rustc_data_structures::unord::UnordMap;
3637
use rustc_errors::{Diag, ErrorGuaranteed};
3738
use rustc_hir::LangItem;
3839
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
@@ -169,7 +170,7 @@ pub struct ResolverGlobalCtxt {
169170
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
170171
pub expn_that_defined: FxHashMap<LocalDefId, ExpnId>,
171172
pub effective_visibilities: EffectiveVisibilities,
172-
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
173+
pub extern_crate_map: UnordMap<LocalDefId, CrateNum>,
173174
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
174175
pub module_children: LocalDefIdMap<Vec<ModChild>>,
175176
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,18 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
178178
),
179179
};
180180

181-
(
181+
Some((
182182
FieldIdx::from_usize(child_field_idx + num_args),
183183
(
184184
FieldIdx::from_usize(parent_field_idx + num_args),
185185
parent_capture_ty,
186186
peel_deref,
187187
child_precise_captures,
188188
),
189-
)
189+
))
190190
},
191191
)
192+
.flatten()
192193
.collect();
193194

194195
if coroutine_kind == ty::ClosureKind::FnOnce {
@@ -312,10 +313,46 @@ impl<'tcx> MutVisitor<'tcx> for MakeByMoveBody<'tcx> {
312313
self.super_place(place, context, location);
313314
}
314315

316+
fn visit_statement(&mut self, statement: &mut mir::Statement<'tcx>, location: mir::Location) {
317+
// Remove fake borrows of closure captures if that capture has been
318+
// replaced with a by-move version of that capture.
319+
//
320+
// For example, imagine we capture `Foo` in the parent and `&Foo`
321+
// in the child. We will emit two fake borrows like:
322+
//
323+
// ```
324+
// _2 = &fake shallow (*(_1.0: &Foo));
325+
// _3 = &fake shallow (_1.0: &Foo);
326+
// ```
327+
//
328+
// However, since this transform is responsible for replacing
329+
// `_1.0: &Foo` with `_1.0: Foo`, that makes the second fake borrow
330+
// obsolete, and we should replace it with a nop.
331+
//
332+
// As a side-note, we don't actually even care about fake borrows
333+
// here at all since they're fully a MIR borrowck artifact, and we
334+
// don't need to borrowck by-move MIR bodies. But it's best to preserve
335+
// as much as we can between these two bodies :)
336+
if let mir::StatementKind::Assign(box (_, rvalue)) = &statement.kind
337+
&& let mir::Rvalue::Ref(_, mir::BorrowKind::Fake(mir::FakeBorrowKind::Shallow), place) =
338+
rvalue
339+
&& let mir::PlaceRef {
340+
local: ty::CAPTURE_STRUCT_LOCAL,
341+
projection: [mir::ProjectionElem::Field(idx, _)],
342+
} = place.as_ref()
343+
&& let Some(&(_, _, true, _)) = self.field_remapping.get(&idx)
344+
{
345+
statement.kind = mir::StatementKind::Nop;
346+
}
347+
348+
self.super_statement(statement, location);
349+
}
350+
315351
fn visit_local_decl(&mut self, local: mir::Local, local_decl: &mut mir::LocalDecl<'tcx>) {
316352
// Replace the type of the self arg.
317353
if local == ty::CAPTURE_STRUCT_LOCAL {
318354
local_decl.ty = self.by_move_coroutine_ty;
319355
}
356+
self.super_local_decl(local, local_decl);
320357
}
321358
}

compiler/rustc_resolve/src/build_reduced_graph.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11151115
}
11161116
});
11171117
} else {
1118+
#[allow(rustc::potential_query_instability)] // FIXME
11181119
for ident in single_imports.iter().cloned() {
11191120
let result = self.r.maybe_resolve_ident_in_module(
11201121
ModuleOrUniformRoot::Module(module),

compiler/rustc_resolve/src/diagnostics.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::{
66
};
77
use rustc_ast_pretty::pprust;
88
use rustc_data_structures::fx::FxHashSet;
9+
use rustc_data_structures::unord::UnordSet;
910
use rustc_errors::codes::*;
1011
use rustc_errors::{
1112
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
@@ -1467,6 +1468,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14671468
return;
14681469
}
14691470

1471+
#[allow(rustc::potential_query_instability)] // FIXME
14701472
let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
14711473
if unused_ident.name == ident.name { Some((def_id, unused_ident)) } else { None }
14721474
});
@@ -2863,18 +2865,11 @@ fn show_candidates(
28632865
} else {
28642866
// Get the unique item kinds and if there's only one, we use the right kind name
28652867
// instead of the more generic "items".
2866-
let mut kinds = accessible_path_strings
2868+
let kinds = accessible_path_strings
28672869
.iter()
28682870
.map(|(_, descr, _, _, _)| *descr)
2869-
.collect::<FxHashSet<&str>>()
2870-
.into_iter();
2871-
let kind = if let Some(kind) = kinds.next()
2872-
&& let None = kinds.next()
2873-
{
2874-
kind
2875-
} else {
2876-
"item"
2877-
};
2871+
.collect::<UnordSet<&str>>();
2872+
let kind = if let Some(kind) = kinds.get_only() { kind } else { "item" };
28782873
let s = if kind.ends_with('s') { "es" } else { "s" };
28792874

28802875
("one of these", kind, s, String::new(), "")

compiler/rustc_resolve/src/ident.rs

+1
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
946946

947947
// Check if one of single imports can still define the name,
948948
// if it can then our result is not determined and can be invalidated.
949+
#[allow(rustc::potential_query_instability)] // FIXME
949950
for single_import in &resolution.single_imports {
950951
if ignore_import == Some(*single_import) {
951952
// This branch handles a cycle in single imports.

compiler/rustc_resolve/src/late.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_ast::visit::{
1818
};
1919
use rustc_ast::*;
2020
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
21+
use rustc_data_structures::unord::{UnordMap, UnordSet};
2122
use rustc_errors::codes::*;
2223
use rustc_errors::{
2324
Applicability, DiagArgValue, ErrorGuaranteed, IntoDiagArg, StashKey, Suggestions,
@@ -47,8 +48,6 @@ mod diagnostics;
4748

4849
type Res = def::Res<NodeId>;
4950

50-
type IdentMap<T> = FxHashMap<Ident, T>;
51-
5251
use diagnostics::{ElisionFnParameter, LifetimeElisionCandidate, MissingLifetime};
5352

5453
#[derive(Copy, Clone, Debug)]
@@ -273,8 +272,8 @@ impl RibKind<'_> {
273272
/// resolving, the name is looked up from inside out.
274273
#[derive(Debug)]
275274
pub(crate) struct Rib<'ra, R = Res> {
276-
pub bindings: IdentMap<R>,
277-
pub patterns_with_skipped_bindings: FxHashMap<DefId, Vec<(Span, Result<(), ErrorGuaranteed>)>>,
275+
pub bindings: FxHashMap<Ident, R>,
276+
pub patterns_with_skipped_bindings: UnordMap<DefId, Vec<(Span, Result<(), ErrorGuaranteed>)>>,
278277
pub kind: RibKind<'ra>,
279278
}
280279

@@ -1605,12 +1604,12 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16051604
// for better diagnostics.
16061605
let mut forward_ty_ban_rib_const_param_ty = Rib {
16071606
bindings: forward_ty_ban_rib.bindings.clone(),
1608-
patterns_with_skipped_bindings: FxHashMap::default(),
1607+
patterns_with_skipped_bindings: Default::default(),
16091608
kind: RibKind::ForwardGenericParamBan(ForwardGenericParamBanReason::ConstParamTy),
16101609
};
16111610
let mut forward_const_ban_rib_const_param_ty = Rib {
16121611
bindings: forward_const_ban_rib.bindings.clone(),
1613-
patterns_with_skipped_bindings: FxHashMap::default(),
1612+
patterns_with_skipped_bindings: Default::default(),
16141613
kind: RibKind::ForwardGenericParamBan(ForwardGenericParamBanReason::ConstParamTy),
16151614
};
16161615
// We'll ban these with a `ConstParamTy` rib, so just clear these ribs for better
@@ -2334,7 +2333,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
23342333
let local_candidates = self.lifetime_elision_candidates.take();
23352334

23362335
if let Some(candidates) = local_candidates {
2337-
let distinct: FxHashSet<_> = candidates.iter().map(|(res, _)| *res).collect();
2336+
let distinct: UnordSet<_> = candidates.iter().map(|(res, _)| *res).collect();
23382337
let lifetime_count = distinct.len();
23392338
if lifetime_count != 0 {
23402339
parameter_info.push(ElisionFnParameter {
@@ -2358,14 +2357,13 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
23582357
}
23592358
}));
23602359
}
2361-
let mut distinct_iter = distinct.into_iter();
2362-
if let Some(res) = distinct_iter.next() {
2360+
if !distinct.is_empty() {
23632361
match elision_lifetime {
23642362
// We are the first parameter to bind lifetimes.
23652363
Elision::None => {
2366-
if distinct_iter.next().is_none() {
2364+
if let Some(res) = distinct.get_only() {
23672365
// We have a single lifetime => success.
2368-
elision_lifetime = Elision::Param(res)
2366+
elision_lifetime = Elision::Param(*res)
23692367
} else {
23702368
// We have multiple lifetimes => error.
23712369
elision_lifetime = Elision::Err;
@@ -2890,6 +2888,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28902888
break;
28912889
}
28922890

2891+
#[allow(rustc::potential_query_instability)] // FIXME
28932892
seen_bindings
28942893
.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
28952894
}
@@ -4004,7 +4003,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
40044003
}
40054004
}
40064005

4007-
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap<Res> {
4006+
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut FxHashMap<Ident, Res> {
40084007
&mut self.ribs[ns].last_mut().unwrap().bindings
40094008
}
40104009

@@ -5202,6 +5201,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
52025201
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
52035202
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
52045203
visit::walk_crate(&mut late_resolution_visitor, krate);
5204+
#[allow(rustc::potential_query_instability)] // FIXME
52055205
for (id, span) in late_resolution_visitor.diag_metadata.unused_labels.iter() {
52065206
self.lint_buffer.buffer_lint(
52075207
lint::builtin::UNUSED_LABELS,

0 commit comments

Comments
 (0)