diff options
| author | bors <bors@rust-lang.org> | 2022-11-22 10:17:09 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-11-22 10:17:09 +0000 |
| commit | b7463e8bdb7b039e75241dcc4ea65bbd24d5c608 (patch) | |
| tree | 837bf66b75c278b9baeb86df6ed75a3230f62fef /compiler/rustc_middle | |
| parent | a78c9bee4d9d51a3891bd8ecae1f28a93b83653b (diff) | |
| parent | 7a5376d23cb3324edcfaa278a9387898960d5f7c (diff) | |
Auto merge of #103578 - petrochenkov:nofict, r=nagisa
Unreserve braced enum variants in value namespace
With this PR braced enum variants (`enum E { V { /*...*/ } }`) no longer take a slot in value namespace, so the special case mentioned in the note in https://github.com/rust-lang/rfcs/blob/master/text/1506-adt-kinds.md#braced-structs is removed.
Report - https://github.com/rust-lang/rust/pull/103578#issuecomment-1292594900.
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/hir/map/mod.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/mod.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/thir.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/adt.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 59 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/print/pretty.rs | 8 |
6 files changed, 42 insertions, 66 deletions
diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index e14ea7be9cf..d4456adf201 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -245,15 +245,15 @@ impl<'hir> Map<'hir> { }, Node::Variant(_) => DefKind::Variant, Node::Ctor(variant_data) => { - // FIXME(eddyb) is this even possible, if we have a `Node::Ctor`? - assert_ne!(variant_data.ctor_hir_id(), None); - let ctor_of = match self.find(self.get_parent_node(hir_id)) { Some(Node::Item(..)) => def::CtorOf::Struct, Some(Node::Variant(..)) => def::CtorOf::Variant, _ => unreachable!(), }; - DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data)) + match variant_data.ctor_kind() { + Some(kind) => DefKind::Ctor(ctor_of, kind), + None => bug!("constructor node without a constructor"), + } } Node::AnonConst(_) => { let inline = match self.find(self.get_parent_node(hir_id)) { diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 8b70eabb105..364c1b375ae 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2115,10 +2115,10 @@ impl<'tcx> Debug for Rvalue<'tcx> { .print_def_path(variant_def.def_id, substs)? .into_buffer(); - match variant_def.ctor_kind { - CtorKind::Const => fmt.write_str(&name), - CtorKind::Fn => fmt_tuple(fmt, &name), - CtorKind::Fictive => { + match variant_def.ctor_kind() { + Some(CtorKind::Const) => fmt.write_str(&name), + Some(CtorKind::Fn) => fmt_tuple(fmt, &name), + None => { let mut struct_fmt = fmt.debug_struct(&name); for (field, place) in iter::zip(&variant_def.fields, places) { struct_fmt.field(field.name.as_str(), place); @@ -2955,14 +2955,14 @@ fn pretty_print_const_value<'tcx>( let cx = cx.print_value_path(variant_def.def_id, substs)?; fmt.write_str(&cx.into_buffer())?; - match variant_def.ctor_kind { - CtorKind::Const => {} - CtorKind::Fn => { + match variant_def.ctor_kind() { + Some(CtorKind::Const) => {} + Some(CtorKind::Fn) => { fmt.write_str("(")?; comma_sep(fmt, fields)?; fmt.write_str(")")?; } - CtorKind::Fictive => { + None => { fmt.write_str(" {{ ")?; let mut first = true; for (field_def, field) in iter::zip(&variant_def.fields, fields) diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index ea7a507d7a4..8bef9dfe099 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -10,7 +10,6 @@ use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_hir as hir; -use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefId; use rustc_hir::RangeEnd; use rustc_index::newtype_index; @@ -751,7 +750,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> { // Only for Adt we can have `S {...}`, // which we handle separately here. - if variant.ctor_kind == CtorKind::Fictive { + if variant.ctor.is_none() { write!(f, " {{ ")?; let mut printed = 0; diff --git a/compiler/rustc_middle/src/ty/adt.rs b/compiler/rustc_middle/src/ty/adt.rs index 137b59cf6c2..6b6aa40a160 100644 --- a/compiler/rustc_middle/src/ty/adt.rs +++ b/compiler/rustc_middle/src/ty/adt.rs @@ -230,7 +230,7 @@ impl AdtDefData { AdtKind::Struct => AdtFlags::IS_STRUCT, }; - if kind == AdtKind::Struct && variants[VariantIdx::new(0)].ctor_def_id.is_some() { + if kind == AdtKind::Struct && variants[VariantIdx::new(0)].ctor.is_some() { flags |= AdtFlags::HAS_CTOR; } @@ -386,11 +386,9 @@ impl<'tcx> AdtDef<'tcx> { // Baz = 3, // } // ``` - if self - .variants() - .iter() - .any(|v| matches!(v.discr, VariantDiscr::Explicit(_)) && v.ctor_kind != CtorKind::Const) - { + if self.variants().iter().any(|v| { + matches!(v.discr, VariantDiscr::Explicit(_)) && v.ctor_kind() != Some(CtorKind::Const) + }) { return false; } self.variants().iter().all(|v| v.fields.is_empty()) @@ -405,7 +403,7 @@ impl<'tcx> AdtDef<'tcx> { pub fn variant_with_ctor_id(self, cid: DefId) -> &'tcx VariantDef { self.variants() .iter() - .find(|v| v.ctor_def_id == Some(cid)) + .find(|v| v.ctor_def_id() == Some(cid)) .expect("variant_with_ctor_id: unknown variant") } @@ -422,7 +420,7 @@ impl<'tcx> AdtDef<'tcx> { pub fn variant_index_with_ctor_id(self, cid: DefId) -> VariantIdx { self.variants() .iter_enumerated() - .find(|(_, v)| v.ctor_def_id == Some(cid)) + .find(|(_, v)| v.ctor_def_id() == Some(cid)) .expect("variant_index_with_ctor_id: unknown variant") .0 } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8bac76d559f..3f26d337d45 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1812,15 +1812,13 @@ pub struct VariantDef { pub def_id: DefId, /// `DefId` that identifies the variant's constructor. /// If this variant is a struct variant, then this is `None`. - pub ctor_def_id: Option<DefId>, + pub ctor: Option<(CtorKind, DefId)>, /// Variant or struct name. pub name: Symbol, /// Discriminant of this variant. pub discr: VariantDiscr, /// Fields of this variant. pub fields: Vec<FieldDef>, - /// Type of constructor of variant. - pub ctor_kind: CtorKind, /// Flags of the variant (e.g. is field list non-exhaustive)? flags: VariantFlags, } @@ -1845,19 +1843,18 @@ impl VariantDef { pub fn new( name: Symbol, variant_did: Option<DefId>, - ctor_def_id: Option<DefId>, + ctor: Option<(CtorKind, DefId)>, discr: VariantDiscr, fields: Vec<FieldDef>, - ctor_kind: CtorKind, adt_kind: AdtKind, parent_did: DefId, recovered: bool, is_field_list_non_exhaustive: bool, ) -> Self { debug!( - "VariantDef::new(name = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?}, - fields = {:?}, ctor_kind = {:?}, adt_kind = {:?}, parent_did = {:?})", - name, variant_did, ctor_def_id, discr, fields, ctor_kind, adt_kind, parent_did, + "VariantDef::new(name = {:?}, variant_did = {:?}, ctor = {:?}, discr = {:?}, + fields = {:?}, adt_kind = {:?}, parent_did = {:?})", + name, variant_did, ctor, discr, fields, adt_kind, parent_did, ); let mut flags = VariantFlags::NO_VARIANT_FLAGS; @@ -1869,15 +1866,7 @@ impl VariantDef { flags |= VariantFlags::IS_RECOVERED; } - VariantDef { - def_id: variant_did.unwrap_or(parent_did), - ctor_def_id, - name, - discr, - fields, - ctor_kind, - flags, - } + VariantDef { def_id: variant_did.unwrap_or(parent_did), ctor, name, discr, fields, flags } } /// Is this field list non-exhaustive? @@ -1896,6 +1885,16 @@ impl VariantDef { pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident { Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap()) } + + #[inline] + pub fn ctor_kind(&self) -> Option<CtorKind> { + self.ctor.map(|(kind, _)| kind) + } + + #[inline] + pub fn ctor_def_id(&self) -> Option<DefId> { + self.ctor.map(|(_, def_id)| def_id) + } } impl PartialEq for VariantDef { @@ -1908,26 +1907,8 @@ impl PartialEq for VariantDef { // definition of `VariantDef` changes, a compile-error will be produced, // reminding us to revisit this assumption. - let Self { - def_id: lhs_def_id, - ctor_def_id: _, - name: _, - discr: _, - fields: _, - ctor_kind: _, - flags: _, - } = &self; - - let Self { - def_id: rhs_def_id, - ctor_def_id: _, - name: _, - discr: _, - fields: _, - ctor_kind: _, - flags: _, - } = other; - + let Self { def_id: lhs_def_id, ctor: _, name: _, discr: _, fields: _, flags: _ } = &self; + let Self { def_id: rhs_def_id, ctor: _, name: _, discr: _, fields: _, flags: _ } = other; lhs_def_id == rhs_def_id } } @@ -1944,9 +1925,7 @@ impl Hash for VariantDef { // of `VariantDef` changes, a compile-error will be produced, reminding // us to revisit this assumption. - let Self { def_id, ctor_def_id: _, name: _, discr: _, fields: _, ctor_kind: _, flags: _ } = - &self; - + let Self { def_id, ctor: _, name: _, discr: _, fields: _, flags: _ } = &self; def_id.hash(s) } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index a0c076cbbb2..d828d08805e 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1487,12 +1487,12 @@ pub trait PrettyPrinter<'tcx>: contents.variant.expect("destructed const of adt without variant idx"); let variant_def = &def.variant(variant_idx); p!(print_value_path(variant_def.def_id, substs)); - match variant_def.ctor_kind { - CtorKind::Const => {} - CtorKind::Fn => { + match variant_def.ctor_kind() { + Some(CtorKind::Const) => {} + Some(CtorKind::Fn) => { p!("(", comma_sep(fields), ")"); } - CtorKind::Fictive => { + None => { p!(" {{ "); let mut first = true; for (field_def, field) in iter::zip(&variant_def.fields, fields) { |
