Skip to content
Permalink
Browse files

Slightly more uniform treatment of struct and variant constructors

  • Loading branch information...
petrochenkov committed Mar 24, 2019
1 parent 7f5a8dc commit 5bcf9f4f119b8109c6c642dcfeb63fb39c26950a
@@ -37,13 +37,11 @@ pub enum NonMacroAttrKind {
pub enum Def {
// Type namespace
Mod(DefId),
/// `DefId` refers to `NodeId` of the struct. `Def::VariantCtor` represents the constructor of
/// a struct.
/// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
Struct(DefId),
Union(DefId),
Enum(DefId),
/// `DefId` refers to the `NodeId` of the variant. `Def::VariantCtor` represents the
/// constructor of an enum variant.
/// `DefId` refers to the variant itself, `Def::Ctor` refers to its constructor if it exists.
Variant(DefId),
Trait(DefId),
/// `existential type Foo: Bar;`
@@ -65,7 +63,7 @@ pub enum Def {
Const(DefId),
ConstParam(DefId),
Static(DefId, bool /* is_mutbl */),
/// `DefId` refers to `NodeId` of the struct or enum variant's constructor.
/// `DefId` refers to the struct or enum variant's constructor.
Ctor(hir::CtorOf, DefId, CtorKind),
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
Method(DefId),
@@ -2322,8 +2322,8 @@ impl<'a, 'gcx, 'tcx> AdtDef {
pub fn variant_of_def(&self, def: Def) -> &VariantDef {
match def {
Def::Variant(vid) => self.variant_with_id(vid),
Def::Ctor(hir::CtorOf::Variant, cid, ..) => self.variant_with_ctor_id(cid),
Def::Struct(..) | Def::Ctor(..) | Def::Union(..) |
Def::Ctor(_, cid, ..) => self.variant_with_ctor_id(cid),
Def::Struct(..) | Def::Union(..) |
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) |
Def::SelfCtor(..) => self.non_enum_variant(),
_ => bug!("unexpected def {:?} in variant_of_def", def)
@@ -261,10 +261,8 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
// Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
expr_ty.ty_adt_def().and_then(|adt_def| {
match path.def {
Def::Ctor(hir::CtorOf::Variant, variant_ctor_id, CtorKind::Fn) => {
Some((adt_def, adt_def.variant_index_with_ctor_id(variant_ctor_id)))
}
Def::Ctor(hir::CtorOf::Struct, _, CtorKind::Fn) |
Def::Ctor(_, ctor_id, CtorKind::Fn) =>
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id))),
Def::SelfCtor(..) => Some((adt_def, VariantIdx::new(0))),
_ => None,
}
@@ -2,7 +2,7 @@ use std::cmp::Reverse;

use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
use log::debug;
use rustc::hir::{self, def::*};
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def::Namespace::*;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use rustc::session::config::nightly_options;
@@ -417,7 +417,7 @@ impl<'a> Resolver<'a> {
}
(Def::Union(..), _) |
(Def::Variant(..), _) |
(Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Fictive), _) if ns == ValueNS => {
(Def::Ctor(_, _, CtorKind::Fictive), _) if ns == ValueNS => {
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?",
path_str));
}
@@ -807,13 +807,13 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
report_unexpected_variant_def(tcx, &def, pat.span, qpath);
return tcx.types.err;
}
Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Fictive) |
Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Fn) => {
Def::Ctor(_, _, CtorKind::Fictive) |
Def::Ctor(_, _, CtorKind::Fn) => {
report_unexpected_variant_def(tcx, &def, pat.span, qpath);
return tcx.types.err;
}
Def::Ctor(_, _, CtorKind::Const) | Def::SelfCtor(..) | Def::Const(..) |
Def::AssociatedConst(..) => {} // OK
Def::Ctor(_, _, CtorKind::Const) | Def::SelfCtor(..) |
Def::Const(..) | Def::AssociatedConst(..) => {} // OK
_ => bug!("unexpected pattern definition: {:?}", def)
}

@@ -4238,7 +4238,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.set_tainted_by_errors();
tcx.types.err
}
Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Fictive) => {
Def::Ctor(_, _, CtorKind::Fictive) => {
report_unexpected_variant_def(tcx, &def, expr.span, qpath);
tcx.types.err
}

0 comments on commit 5bcf9f4

Please sign in to comment.
You can’t perform that action at this time.