Skip to content

Commit 3694a6b

Browse files
committed
Auto merge of #119170 - matthiaskrgr:rollup-nllgdf2, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #119135 (Fix crash due to `CrateItem::kind()` not handling constructors) - #119141 (Add method to get instance instantiation arguments) - #119145 (Give `VariantData::Struct` named fields, to clairfy `recovered`.) - #119167 (E0761: module directory has .rs suffix) - #119168 (resolve: Stop feeding visibilities for import list stems) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5ac4c8a + c36bb5d commit 3694a6b

File tree

30 files changed

+182
-45
lines changed

30 files changed

+182
-45
lines changed

compiler/rustc_ast/src/ast.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,11 @@ pub enum VariantData {
27882788
/// Struct variant.
27892789
///
27902790
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
2791-
Struct(ThinVec<FieldDef>, bool),
2791+
Struct {
2792+
fields: ThinVec<FieldDef>,
2793+
// FIXME: investigate making this a `Option<ErrorGuaranteed>`
2794+
recovered: bool,
2795+
},
27922796
/// Tuple variant.
27932797
///
27942798
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
@@ -2803,15 +2807,15 @@ impl VariantData {
28032807
/// Return the fields of this variant.
28042808
pub fn fields(&self) -> &[FieldDef] {
28052809
match self {
2806-
VariantData::Struct(fields, ..) | VariantData::Tuple(fields, _) => fields,
2810+
VariantData::Struct { fields, .. } | VariantData::Tuple(fields, _) => fields,
28072811
_ => &[],
28082812
}
28092813
}
28102814

28112815
/// Return the `NodeId` of this variant's constructor, if it has one.
28122816
pub fn ctor_node_id(&self) -> Option<NodeId> {
28132817
match *self {
2814-
VariantData::Struct(..) => None,
2818+
VariantData::Struct { .. } => None,
28152819
VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
28162820
}
28172821
}

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ pub fn noop_visit_where_predicate<T: MutVisitor>(pred: &mut WherePredicate, vis:
976976

977977
pub fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut T) {
978978
match vdata {
979-
VariantData::Struct(fields, ..) => {
979+
VariantData::Struct { fields, .. } => {
980980
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
981981
}
982982
VariantData::Tuple(fields, id) => {

compiler/rustc_ast_lowering/src/item.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
661661
vdata: &VariantData,
662662
) -> hir::VariantData<'hir> {
663663
match vdata {
664-
VariantData::Struct(fields, recovered) => hir::VariantData::Struct(
665-
self.arena
664+
VariantData::Struct { fields, recovered } => hir::VariantData::Struct {
665+
fields: self
666+
.arena
666667
.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f))),
667-
*recovered,
668-
),
668+
recovered: *recovered,
669+
},
669670
VariantData::Tuple(fields, id) => {
670671
let ctor_id = self.lower_node_id(*id);
671672
self.alias_attrs(ctor_id, parent_id);

compiler/rustc_ast_passes/src/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10001000
}
10011001
}
10021002
ItemKind::Struct(vdata, generics) => match vdata {
1003-
VariantData::Struct(fields, ..) => {
1003+
VariantData::Struct { fields, .. } => {
10041004
self.visit_vis(&item.vis);
10051005
self.visit_ident(item.ident);
10061006
self.visit_generics(generics);
@@ -1016,7 +1016,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10161016
self.dcx().emit_err(errors::FieldlessUnion { span: item.span });
10171017
}
10181018
match vdata {
1019-
VariantData::Struct(fields, ..) => {
1019+
VariantData::Struct { fields, .. } => {
10201020
self.visit_vis(&item.vis);
10211021
self.visit_ident(item.ident);
10221022
self.visit_generics(generics);

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<'a> State<'a> {
500500
self.end();
501501
self.end(); // Close the outer-box.
502502
}
503-
ast::VariantData::Struct(fields, ..) => {
503+
ast::VariantData::Struct { fields, .. } => {
504504
self.print_where_clause(&generics.where_clause);
505505
self.print_record_struct_body(fields, span);
506506
}

compiler/rustc_builtin_macros/src/deriving/clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn cs_clone(
188188
}
189189

190190
let expr = match *vdata {
191-
VariantData::Struct(..) => {
191+
VariantData::Struct { .. } => {
192192
let fields = all_fields
193193
.iter()
194194
.map(|field| {

compiler/rustc_builtin_macros/src/deriving/debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
7171
(false, 0)
7272
}
7373
ast::VariantData::Tuple(..) => (false, 1),
74-
ast::VariantData::Struct(..) => (true, 2),
74+
ast::VariantData::Struct { .. } => (true, 2),
7575
};
7676

7777
// The number of fields that can be handled without an array.
@@ -226,7 +226,7 @@ fn show_fieldless_enum(
226226
debug_assert!(fields.is_empty());
227227
cx.pat_tuple_struct(span, variant_path, ThinVec::new())
228228
}
229-
ast::VariantData::Struct(fields, _) => {
229+
ast::VariantData::Struct { fields, .. } => {
230230
debug_assert!(fields.is_empty());
231231
cx.pat_struct(span, variant_path, ThinVec::new())
232232
}

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ impl<'a> TraitDef<'a> {
14851485

14861486
let struct_path = struct_path.clone();
14871487
match *struct_def {
1488-
VariantData::Struct(..) => {
1488+
VariantData::Struct { .. } => {
14891489
let field_pats = pieces_iter
14901490
.map(|(sp, ident, pat)| {
14911491
if ident.is_none() {

compiler/rustc_error_codes/src/error_codes/E0761.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn foo() {}
1515
1616
mod ambiguous_module; // error: file for module `ambiguous_module`
1717
// found at both ambiguous_module.rs and
18-
// ambiguous_module.rs/mod.rs
18+
// ambiguous_module/mod.rs
1919
```
2020

2121
Please remove this ambiguity by deleting/renaming one of the candidate files.

compiler/rustc_expand/src/placeholders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub fn placeholder(
174174
}]),
175175
AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
176176
attrs: Default::default(),
177-
data: ast::VariantData::Struct(Default::default(), false),
177+
data: ast::VariantData::Struct { fields: Default::default(), recovered: false },
178178
disr_expr: None,
179179
id,
180180
ident,

compiler/rustc_hir/src/def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ impl CtorKind {
603603
match *vdata {
604604
ast::VariantData::Tuple(_, node_id) => Some((CtorKind::Fn, node_id)),
605605
ast::VariantData::Unit(node_id) => Some((CtorKind::Const, node_id)),
606-
ast::VariantData::Struct(..) => None,
606+
ast::VariantData::Struct { .. } => None,
607607
}
608608
}
609609
}

compiler/rustc_hir/src/hir.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,11 @@ pub enum VariantData<'hir> {
28482848
/// A struct variant.
28492849
///
28502850
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
2851-
Struct(&'hir [FieldDef<'hir>], /* recovered */ bool),
2851+
Struct {
2852+
fields: &'hir [FieldDef<'hir>],
2853+
// FIXME: investigate making this a `Option<ErrorGuaranteed>`
2854+
recovered: bool,
2855+
},
28522856
/// A tuple variant.
28532857
///
28542858
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
@@ -2863,7 +2867,7 @@ impl<'hir> VariantData<'hir> {
28632867
/// Return the fields of this variant.
28642868
pub fn fields(&self) -> &'hir [FieldDef<'hir>] {
28652869
match *self {
2866-
VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, ..) => fields,
2870+
VariantData::Struct { fields, .. } | VariantData::Tuple(fields, ..) => fields,
28672871
_ => &[],
28682872
}
28692873
}
@@ -2872,7 +2876,7 @@ impl<'hir> VariantData<'hir> {
28722876
match *self {
28732877
VariantData::Tuple(_, hir_id, def_id) => Some((CtorKind::Fn, hir_id, def_id)),
28742878
VariantData::Unit(hir_id, def_id) => Some((CtorKind::Const, hir_id, def_id)),
2875-
VariantData::Struct(..) => None,
2879+
VariantData::Struct { .. } => None,
28762880
}
28772881
}
28782882

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ fn convert_variant(
814814
})
815815
.collect();
816816
let recovered = match def {
817-
hir::VariantData::Struct(_, r) => *r,
817+
hir::VariantData::Struct { recovered, .. } => *recovered,
818818
_ => false,
819819
};
820820
ty::VariantDef::new(

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
481481
},
482482

483483
Node::Ctor(def) | Node::Variant(Variant { data: def, .. }) => match def {
484-
VariantData::Unit(..) | VariantData::Struct(..) => {
484+
VariantData::Unit(..) | VariantData::Struct { .. } => {
485485
tcx.type_of(tcx.hir().get_parent_item(hir_id)).instantiate_identity()
486486
}
487487
VariantData::Tuple(..) => {

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ impl<'a> State<'a> {
741741
self.end();
742742
self.end() // close the outer-box
743743
}
744-
hir::VariantData::Struct(..) => {
744+
hir::VariantData::Struct { .. } => {
745745
self.print_where_clause(generics);
746746
self.nbsp();
747747
self.bopen();

compiler/rustc_parse/src/parser/item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ impl<'a> Parser<'a> {
14841484
(thin_vec![], true)
14851485
}
14861486
};
1487-
VariantData::Struct(fields, recovered)
1487+
VariantData::Struct { fields, recovered }
14881488
} else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) {
14891489
let body = match this.parse_tuple_struct_body() {
14901490
Ok(body) => body,
@@ -1569,7 +1569,7 @@ impl<'a> Parser<'a> {
15691569
class_name.span,
15701570
generics.where_clause.has_where_token,
15711571
)?;
1572-
VariantData::Struct(fields, recovered)
1572+
VariantData::Struct { fields, recovered }
15731573
}
15741574
// No `where` so: `struct Foo<T>;`
15751575
} else if self.eat(&token::Semi) {
@@ -1581,7 +1581,7 @@ impl<'a> Parser<'a> {
15811581
class_name.span,
15821582
generics.where_clause.has_where_token,
15831583
)?;
1584-
VariantData::Struct(fields, recovered)
1584+
VariantData::Struct { fields, recovered }
15851585
// Tuple-style struct definition with optional where-clause.
15861586
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
15871587
let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
@@ -1610,14 +1610,14 @@ impl<'a> Parser<'a> {
16101610
class_name.span,
16111611
generics.where_clause.has_where_token,
16121612
)?;
1613-
VariantData::Struct(fields, recovered)
1613+
VariantData::Struct { fields, recovered }
16141614
} else if self.token == token::OpenDelim(Delimiter::Brace) {
16151615
let (fields, recovered) = self.parse_record_struct_body(
16161616
"union",
16171617
class_name.span,
16181618
generics.where_clause.has_where_token,
16191619
)?;
1620-
VariantData::Struct(fields, recovered)
1620+
VariantData::Struct { fields, recovered }
16211621
} else {
16221622
let token_str = super::token_descr(&self.token);
16231623
let msg = format!("expected `where` or `{{` after union name, found {token_str}");

compiler/rustc_resolve/src/build_reduced_graph.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
394394
id: NodeId,
395395
parent_prefix: &[Segment],
396396
nested: bool,
397+
list_stem: bool,
397398
// The whole `use` item
398399
item: &Item,
399400
vis: ty::Visibility,
@@ -404,7 +405,9 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
404405
parent_prefix, use_tree, nested
405406
);
406407

407-
if nested {
408+
// Top level use tree reuses the item's id and list stems reuse their parent
409+
// use tree's ids, so in both cases their visibilities are already filled.
410+
if nested && !list_stem {
408411
self.r.feed_visibility(self.r.local_def_id(id), vis);
409412
}
410413

@@ -592,7 +595,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
592595
for &(ref tree, id) in items {
593596
self.build_reduced_graph_for_use_tree(
594597
// This particular use tree
595-
tree, id, &prefix, true, // The whole `use` item
598+
tree, id, &prefix, true, false, // The whole `use` item
596599
item, vis, root_span,
597600
);
598601
}
@@ -613,6 +616,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
613616
id,
614617
&prefix,
615618
true,
619+
true,
616620
// The whole `use` item
617621
item,
618622
ty::Visibility::Restricted(
@@ -648,6 +652,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
648652
item.id,
649653
&[],
650654
false,
655+
false,
651656
// The whole `use` item
652657
item,
653658
vis,

compiler/rustc_smir/src/rustc_smir/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
341341
instance.ty(tables.tcx, ParamEnv::reveal_all()).stable(&mut *tables)
342342
}
343343

344+
fn instance_args(&self, def: InstanceDef) -> GenericArgs {
345+
let mut tables = self.0.borrow_mut();
346+
let instance = tables.instances[def];
347+
instance.args.stable(&mut *tables)
348+
}
349+
344350
fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
345351
let mut tables = self.0.borrow_mut();
346352
let instance = tables.instances[def];

compiler/rustc_smir/src/rustc_smir/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1515
use stable_mir::abi::Layout;
1616
use stable_mir::mir::mono::InstanceDef;
1717
use stable_mir::ty::{ConstId, Span};
18-
use stable_mir::ItemKind;
18+
use stable_mir::{CtorKind, ItemKind};
1919
use std::ops::RangeInclusive;
2020
use tracing::debug;
2121

@@ -88,7 +88,6 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
8888
| DefKind::Field
8989
| DefKind::LifetimeParam
9090
| DefKind::Impl { .. }
91-
| DefKind::Ctor(_, _)
9291
| DefKind::GlobalAsm => {
9392
unreachable!("Not a valid item kind: {kind:?}");
9493
}
@@ -97,6 +96,8 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
9796
ItemKind::Const
9897
}
9998
DefKind::Static(_) => ItemKind::Static,
99+
DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
100+
DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
100101
}
101102
}
102103

compiler/stable_mir/src/compiler_interface.rs

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ pub trait Context {
125125
/// Get the instance type with generic substitutions applied and lifetimes erased.
126126
fn instance_ty(&self, instance: InstanceDef) -> Ty;
127127

128+
/// Get the instantiation types.
129+
fn instance_args(&self, def: InstanceDef) -> GenericArgs;
130+
128131
/// Get the instance.
129132
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
130133

compiler/stable_mir/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ pub enum ItemKind {
9191
Fn,
9292
Static,
9393
Const,
94+
Ctor(CtorKind),
95+
}
96+
97+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
98+
pub enum CtorKind {
99+
Const,
100+
Fn,
94101
}
95102

96103
pub type Filename = String;

compiler/stable_mir/src/mir/mono.rs

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ pub enum InstanceKind {
3535
}
3636

3737
impl Instance {
38+
/// Get the arguments this instance was instantiated with.
39+
pub fn args(&self) -> GenericArgs {
40+
with(|cx| cx.instance_args(self.def))
41+
}
42+
3843
/// Get the body of an Instance. The body will be eagerly monomorphized.
3944
pub fn body(&self) -> Option<Body> {
4045
with(|context| context.instance_body(self.def))
@@ -148,6 +153,7 @@ impl Debug for Instance {
148153
f.debug_struct("Instance")
149154
.field("kind", &self.kind)
150155
.field("def", &self.mangled_name())
156+
.field("args", &self.args())
151157
.finish()
152158
}
153159
}

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2479,8 +2479,8 @@ fn clean_variant_data<'tcx>(
24792479
.map(|disr| Discriminant { expr: Some(disr.body), value: disr.def_id.to_def_id() });
24802480

24812481
let kind = match variant {
2482-
hir::VariantData::Struct(..) => VariantKind::Struct(VariantStruct {
2483-
fields: variant.fields().iter().map(|x| clean_field(x, cx)).collect(),
2482+
hir::VariantData::Struct { fields, .. } => VariantKind::Struct(VariantStruct {
2483+
fields: fields.iter().map(|x| clean_field(x, cx)).collect(),
24842484
}),
24852485
hir::VariantData::Tuple(..) => {
24862486
VariantKind::Tuple(variant.fields().iter().map(|x| clean_field(x, cx)).collect())

src/tools/clippy/clippy_lints/src/item_name_repetitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl LateLintPass<'_> for ItemNameRepetitions {
436436
{
437437
match item.kind {
438438
ItemKind::Enum(def, _) => check_variant(cx, self.enum_threshold, &def, item_name, item.span),
439-
ItemKind::Struct(VariantData::Struct(fields, _), _) => {
439+
ItemKind::Struct(VariantData::Struct { fields, .. }, _) => {
440440
check_fields(cx, self.struct_threshold, item, fields);
441441
},
442442
_ => (),

0 commit comments

Comments
 (0)