Skip to content

Commit 8599bff

Browse files
committed
Auto merge of #82281 - Dylan-DPC:rollup-raob2tu, r=Dylan-DPC
Rollup of 10 pull requests Successful merges: - #79747 (Add explanations and suggestions to `irrefutable_let_patterns` lint) - #81496 (name async generators something more human friendly in type error diagnostic) - #81873 (Add Mutex::unlock) - #82093 (Add tests for Atomic*::fetch_{min,max}) - #82238 (ast: Keep expansion status for out-of-line module items) - #82245 (Do not ICE when evaluating locals' types of invalid `yield`) - #82259 (Fix popping singleton paths in when generating E0433) - #82261 (rustdoc: Support argument files) - #82274 (libtest: Fix unwrap panic on duplicate TestDesc) - #82275 (Update cargo) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0148b97 + 979b00b commit 8599bff

File tree

63 files changed

+607
-333
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+607
-333
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ checksum = "81a18687293a1546b67c246452202bbbf143d239cb43494cc163da14979082da"
285285

286286
[[package]]
287287
name = "cargo"
288-
version = "0.52.0"
288+
version = "0.53.0"
289289
dependencies = [
290290
"anyhow",
291291
"atty",

compiler/rustc_ast/src/ast.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ pub struct WhereEqPredicate {
486486

487487
#[derive(Clone, Encodable, Decodable, Debug)]
488488
pub struct Crate {
489-
pub module: Mod,
490489
pub attrs: Vec<Attribute>,
490+
pub items: Vec<P<Item>>,
491491
pub span: Span,
492492
/// The order of items in the HIR is unrelated to the order of
493493
/// items in the AST. However, we generate proc macro harnesses
@@ -2299,21 +2299,22 @@ impl FnRetTy {
22992299
}
23002300
}
23012301

2302-
/// Module declaration.
2303-
///
2304-
/// E.g., `mod foo;` or `mod foo { .. }`.
2302+
#[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
2303+
pub enum Inline {
2304+
Yes,
2305+
No,
2306+
}
2307+
2308+
/// Module item kind.
23052309
#[derive(Clone, Encodable, Decodable, Debug)]
2306-
pub struct Mod {
2307-
/// A span from the first token past `{` to the last token until `}`.
2308-
/// For `mod foo;`, the inner span ranges from the first token
2309-
/// to the last token in the external file.
2310-
pub inner: Span,
2311-
/// `unsafe` keyword accepted syntactically for macro DSLs, but not
2312-
/// semantically by Rust.
2313-
pub unsafety: Unsafe,
2314-
pub items: Vec<P<Item>>,
2315-
/// `true` for `mod foo { .. }`; `false` for `mod foo;`.
2316-
pub inline: bool,
2310+
pub enum ModKind {
2311+
/// Module with inlined definition `mod foo { ... }`,
2312+
/// or with definition outlined to a separate file `mod foo;` and already loaded from it.
2313+
/// The inner span is from the first token past `{` to the last token until `}`,
2314+
/// or from the first to the last token in the loaded file.
2315+
Loaded(Vec<P<Item>>, Inline, Span),
2316+
/// Module with definition outlined to a separate file `mod foo;` but not yet loaded from it.
2317+
Unloaded,
23172318
}
23182319

23192320
/// Foreign module declaration.
@@ -2710,7 +2711,9 @@ pub enum ItemKind {
27102711
/// A module declaration (`mod`).
27112712
///
27122713
/// E.g., `mod foo;` or `mod foo { .. }`.
2713-
Mod(Mod),
2714+
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
2715+
/// semantically by Rust.
2716+
Mod(Unsafe, ModKind),
27142717
/// An external module (`extern`).
27152718
///
27162719
/// E.g., `extern {}` or `extern "C" {}`.

compiler/rustc_ast/src/mut_visit.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ pub trait MutVisitor: Sized {
170170
noop_visit_ty_constraint(t, self);
171171
}
172172

173-
fn visit_mod(&mut self, m: &mut Mod) {
174-
noop_visit_mod(m, self);
175-
}
176-
177173
fn visit_foreign_mod(&mut self, nm: &mut ForeignMod) {
178174
noop_visit_foreign_mod(nm, self);
179175
}
@@ -917,7 +913,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
917913
vis.visit_generics(generics);
918914
visit_opt(body, |body| vis.visit_block(body));
919915
}
920-
ItemKind::Mod(m) => vis.visit_mod(m),
916+
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
917+
ModKind::Loaded(items, _inline, inner_span) => {
918+
vis.visit_span(inner_span);
919+
items.flat_map_in_place(|item| vis.flat_map_item(item));
920+
}
921+
ModKind::Unloaded => {}
922+
},
921923
ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm),
922924
ItemKind::GlobalAsm(_ga) => {}
923925
ItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
@@ -998,14 +1000,10 @@ pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
9981000
vis.visit_asyncness(asyncness);
9991001
}
10001002

1001-
pub fn noop_visit_mod<T: MutVisitor>(module: &mut Mod, vis: &mut T) {
1002-
let Mod { inner, unsafety: _, items, inline: _ } = module;
1003-
vis.visit_span(inner);
1004-
items.flat_map_in_place(|item| vis.flat_map_item(item));
1005-
}
1006-
1003+
// FIXME: Avoid visiting the crate as a `Mod` item, flat map only the inner items if possible,
1004+
// or make crate visiting first class if necessary.
10071005
pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
1008-
visit_clobber(krate, |Crate { module, attrs, span, proc_macros }| {
1006+
visit_clobber(krate, |Crate { attrs, items, span, proc_macros }| {
10091007
let item_vis =
10101008
Visibility { kind: VisibilityKind::Public, span: span.shrink_to_lo(), tokens: None };
10111009
let item = P(Item {
@@ -1014,19 +1012,20 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
10141012
id: DUMMY_NODE_ID,
10151013
vis: item_vis,
10161014
span,
1017-
kind: ItemKind::Mod(module),
1015+
kind: ItemKind::Mod(Unsafe::No, ModKind::Loaded(items, Inline::Yes, span)),
10181016
tokens: None,
10191017
});
10201018
let items = vis.flat_map_item(item);
10211019

10221020
let len = items.len();
10231021
if len == 0 {
1024-
let module = Mod { inner: span, unsafety: Unsafe::No, items: vec![], inline: true };
1025-
Crate { module, attrs: vec![], span, proc_macros }
1022+
Crate { attrs: vec![], items: vec![], span, proc_macros }
10261023
} else if len == 1 {
10271024
let Item { attrs, span, kind, .. } = items.into_iter().next().unwrap().into_inner();
10281025
match kind {
1029-
ItemKind::Mod(module) => Crate { module, attrs, span, proc_macros },
1026+
ItemKind::Mod(_, ModKind::Loaded(items, ..)) => {
1027+
Crate { attrs, items, span, proc_macros }
1028+
}
10301029
_ => panic!("visitor converted a module to not a module"),
10311030
}
10321031
} else {

compiler/rustc_ast/src/visit.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a> FnKind<'a> {
7474
/// Each method of the `Visitor` trait is a hook to be potentially
7575
/// overridden. Each method's default implementation recursively visits
7676
/// the substructure of the input via the corresponding `walk` method;
77-
/// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
77+
/// e.g., the `visit_item` method by default calls `visit::walk_item`.
7878
///
7979
/// If you want to ensure that your code handles every variant
8080
/// explicitly, you need to override each method. (And you also need
@@ -87,9 +87,6 @@ pub trait Visitor<'ast>: Sized {
8787
fn visit_ident(&mut self, ident: Ident) {
8888
walk_ident(self, ident);
8989
}
90-
fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
91-
walk_mod(self, m);
92-
}
9390
fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
9491
walk_foreign_item(self, i)
9592
}
@@ -238,14 +235,10 @@ pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
238235
}
239236

240237
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
241-
visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
238+
walk_list!(visitor, visit_item, &krate.items);
242239
walk_list!(visitor, visit_attribute, &krate.attrs);
243240
}
244241

245-
pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
246-
walk_list!(visitor, visit_item, &module.items);
247-
}
248-
249242
pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
250243
for attr in local.attrs.iter() {
251244
visitor.visit_attribute(attr);
@@ -297,7 +290,12 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
297290
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
298291
visitor.visit_fn(kind, item.span, item.id)
299292
}
300-
ItemKind::Mod(ref module) => visitor.visit_mod(module, item.span, &item.attrs, item.id),
293+
ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
294+
ModKind::Loaded(items, _inline, _inner_span) => {
295+
walk_list!(visitor, visit_item, items)
296+
}
297+
ModKind::Unloaded => {}
298+
},
301299
ItemKind::ForeignMod(ref foreign_module) => {
302300
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
303301
}

compiler/rustc_ast_lowering/src/item.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use rustc_span::source_map::{respan, DesugaringKind};
1515
use rustc_span::symbol::{kw, sym, Ident};
1616
use rustc_span::Span;
1717
use rustc_target::spec::abi;
18-
1918
use smallvec::{smallvec, SmallVec};
20-
use std::collections::BTreeSet;
2119
use tracing::debug;
2220

21+
use std::mem;
22+
2323
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2424
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
2525
}
@@ -34,25 +34,6 @@ impl ItemLowerer<'_, '_, '_> {
3434
}
3535

3636
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
37-
fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
38-
let def_id = self.lctx.lower_node_id(n).expect_owner();
39-
40-
self.lctx.modules.insert(
41-
def_id,
42-
hir::ModuleItems {
43-
items: BTreeSet::new(),
44-
trait_items: BTreeSet::new(),
45-
impl_items: BTreeSet::new(),
46-
foreign_items: BTreeSet::new(),
47-
},
48-
);
49-
50-
let old = self.lctx.current_module;
51-
self.lctx.current_module = def_id;
52-
visit::walk_mod(self, m);
53-
self.lctx.current_module = old;
54-
}
55-
5637
fn visit_item(&mut self, item: &'a Item) {
5738
let mut item_hir_id = None;
5839
self.lctx.with_hir_id_owner(item.id, |lctx| {
@@ -67,10 +48,18 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
6748
if let Some(hir_id) = item_hir_id {
6849
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
6950
let this = &mut ItemLowerer { lctx: this };
70-
if let ItemKind::Impl(box ImplKind { ref of_trait, .. }) = item.kind {
71-
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
72-
} else {
73-
visit::walk_item(this, item);
51+
match item.kind {
52+
ItemKind::Mod(..) => {
53+
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
54+
let old_current_module =
55+
mem::replace(&mut this.lctx.current_module, def_id);
56+
visit::walk_item(this, item);
57+
this.lctx.current_module = old_current_module;
58+
}
59+
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
60+
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
61+
}
62+
_ => visit::walk_item(this, item),
7463
}
7564
});
7665
}
@@ -94,13 +83,13 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
9483
let hir_item = lctx.lower_trait_item(item);
9584
let id = hir_item.trait_item_id();
9685
lctx.trait_items.insert(id, hir_item);
97-
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
86+
lctx.modules.entry(lctx.current_module).or_default().trait_items.insert(id);
9887
}
9988
AssocCtxt::Impl => {
10089
let hir_item = lctx.lower_impl_item(item);
10190
let id = hir_item.impl_item_id();
10291
lctx.impl_items.insert(id, hir_item);
103-
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
92+
lctx.modules.entry(lctx.current_module).or_default().impl_items.insert(id);
10493
}
10594
});
10695

@@ -113,7 +102,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
113102
let hir_item = lctx.lower_foreign_item(item);
114103
let id = hir_item.foreign_item_id();
115104
lctx.foreign_items.insert(id, hir_item);
116-
lctx.modules.get_mut(&lctx.current_module).unwrap().foreign_items.insert(id);
105+
lctx.modules.entry(lctx.current_module).or_default().foreign_items.insert(id);
117106
});
118107

119108
visit::walk_foreign_item(self, item);
@@ -157,7 +146,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
157146
&mut self,
158147
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
159148
) -> T {
160-
let old_in_scope_lifetimes = std::mem::replace(&mut self.in_scope_lifetimes, vec![]);
149+
let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]);
161150

162151
// this vector is only used when walking over impl headers,
163152
// input types, and the like, and should not be non-empty in
@@ -172,12 +161,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
172161
res
173162
}
174163

175-
pub(super) fn lower_mod(&mut self, m: &Mod) -> hir::Mod<'hir> {
164+
pub(super) fn lower_mod(&mut self, items: &[P<Item>], inner: Span) -> hir::Mod<'hir> {
176165
hir::Mod {
177-
inner: m.inner,
178-
item_ids: self
179-
.arena
180-
.alloc_from_iter(m.items.iter().flat_map(|x| self.lower_item_id(x))),
166+
inner,
167+
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_id(x))),
181168
}
182169
}
183170

@@ -327,7 +314,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
327314
hir::ItemKind::Fn(sig, generics, body_id)
328315
})
329316
}
330-
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
317+
ItemKind::Mod(_, ref mod_kind) => match mod_kind {
318+
ModKind::Loaded(items, _, inner_span) => {
319+
hir::ItemKind::Mod(self.lower_mod(items, *inner_span))
320+
}
321+
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
322+
},
331323
ItemKind::ForeignMod(ref fm) => {
332324
if fm.abi.is_none() {
333325
self.maybe_lint_missing_abi(span, id, abi::Abi::C);

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
560560
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
561561
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
562562

563-
let module = self.lower_mod(&c.module);
563+
let module = self.lower_mod(&c.items, c.span);
564564
let attrs = self.lower_attrs(&c.attrs);
565565
let body_ids = body_ids(&self.bodies);
566566
let proc_macros =
@@ -608,7 +608,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
608608
fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId {
609609
let id = hir::ItemId { def_id: item.def_id };
610610
self.items.insert(id, item);
611-
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
611+
self.modules.entry(self.current_module).or_default().items.insert(id);
612612
id
613613
}
614614

compiler/rustc_ast_passes/src/ast_validation.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1054,12 +1054,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10541054
walk_list!(self, visit_attribute, &item.attrs);
10551055
return;
10561056
}
1057-
ItemKind::Mod(Mod { inline, unsafety, .. }) => {
1057+
ItemKind::Mod(unsafety, ref mod_kind) => {
10581058
if let Unsafe::Yes(span) = unsafety {
10591059
self.err_handler().span_err(span, "module cannot be declared unsafe");
10601060
}
10611061
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
1062-
if !inline && !self.session.contains_name(&item.attrs, sym::path) {
1062+
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _))
1063+
&& !self.session.contains_name(&item.attrs, sym::path)
1064+
{
10631065
self.check_mod_file_item_asciionly(item.ident);
10641066
}
10651067
}

compiler/rustc_ast_passes/src/node_count.rs

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ impl<'ast> Visitor<'ast> for NodeCounter {
2020
self.count += 1;
2121
walk_ident(self, ident);
2222
}
23-
fn visit_mod(&mut self, m: &Mod, _s: Span, _a: &[Attribute], _n: NodeId) {
24-
self.count += 1;
25-
walk_mod(self, m)
26-
}
2723
fn visit_foreign_item(&mut self, i: &ForeignItem) {
2824
self.count += 1;
2925
walk_foreign_item(self, i)

0 commit comments

Comments
 (0)