1
Fork 0

Unreserve braced enum variants in value namespace

This commit is contained in:
Vadim Petrochenkov 2022-10-25 20:15:15 +04:00
parent 1cbc45942d
commit 7a5376d23c
71 changed files with 364 additions and 642 deletions

View file

@ -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)) {

View file

@ -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)

View file

@ -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;

View file

@ -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
}

View file

@ -1808,15 +1808,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,
}
@ -1841,19 +1839,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;
@ -1865,15 +1862,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?
@ -1892,6 +1881,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 {
@ -1904,26 +1903,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
}
}
@ -1940,9 +1921,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)
}
}

View file

@ -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) {