Skip to content

Commit

Permalink
Auto merge of #31010 - petrochenkov:def, r=arielb1
Browse files Browse the repository at this point in the history
All structs and their constructors are defined as `DefStruct`.
`DefTy` is splitted into `DefEnum` and `DefTyAlias`.
Ad hoc flag `bool is_structure` is removed from `DefVariant`, it was required in one place in resolve and could be obtained by other means.
Flag `bool is_ctor` is removed from `DefFn`, it wasn't really used for constructors outside of metadata decoding.

Observable effects:
More specific error messages are selected in some cases.
Two name resolution bugs fixed (#30992 and FIXME in compile-fail/empty-struct-braces-expr.rs).

Fixes #30992
Closes #30361
  • Loading branch information
bors committed Jan 21, 2016
2 parents 7dce32e + 2084c2c commit 51108b6
Show file tree
Hide file tree
Showing 57 changed files with 725 additions and 671 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/astconv_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Almost certainly this could (and should) be refactored out of existence.
*/

use middle::def;
use middle::def::Def;
use middle::ty::{self, Ty};

use syntax::codemap::Span;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
}
Some(d) => d.full_def()
};
if let def::DefPrimTy(nty) = def {
if let Def::PrimTy(nty) = def {
Some(prim_ty_to_ty(tcx, &path.segments, nty))
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use rustc_data_structures::graph;
use middle::cfg::*;
use middle::def;
use middle::def::Def;
use middle::pat_util;
use middle::ty;
use syntax::ast;
Expand Down Expand Up @@ -591,7 +591,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
}

match self.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
Some(def::DefLabel(loop_id)) => {
Some(Def::Label(loop_id)) => {
for l in &self.loop_scopes {
if l.loop_id == loop_id {
return *l;
Expand Down
24 changes: 12 additions & 12 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use middle::ty::cast::{CastKind};
use middle::const_eval::{self, ConstEvalErr};
use middle::const_eval::ErrKind::IndexOpFeatureGated;
use middle::const_eval::EvalHint::ExprTypeChecked;
use middle::def;
use middle::def::Def;
use middle::def_id::DefId;
use middle::expr_use_visitor as euv;
use middle::infer;
Expand Down Expand Up @@ -610,21 +610,21 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
hir::ExprPath(..) => {
let def = v.tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
match def {
Some(def::DefVariant(_, _, _)) => {
Some(Def::Variant(..)) => {
// Count the discriminator or function pointer.
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
}
Some(def::DefStruct(_)) => {
Some(Def::Struct(..)) => {
if let ty::TyBareFn(..) = node_ty.sty {
// Count the function pointer.
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
}
}
Some(def::DefFn(..)) | Some(def::DefMethod(..)) => {
Some(Def::Fn(..)) | Some(Def::Method(..)) => {
// Count the function pointer.
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
}
Some(def::DefStatic(..)) => {
Some(Def::Static(..)) => {
match v.mode {
Mode::Static | Mode::StaticMut => {}
Mode::Const | Mode::ConstFn => {
Expand All @@ -635,16 +635,16 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
Mode::Var => v.add_qualif(ConstQualif::NOT_CONST)
}
}
Some(def::DefConst(did)) |
Some(def::DefAssociatedConst(did)) => {
Some(Def::Const(did)) |
Some(Def::AssociatedConst(did)) => {
if let Some(expr) = const_eval::lookup_const_by_id(v.tcx, did,
Some(e.id),
None) {
let inner = v.global_expr(Mode::Const, expr);
v.add_qualif(inner);
}
}
Some(def::DefLocal(..)) if v.mode == Mode::ConstFn => {
Some(Def::Local(..)) if v.mode == Mode::ConstFn => {
// Sadly, we can't determine whether the types are zero-sized.
v.add_qualif(ConstQualif::NOT_CONST | ConstQualif::NON_ZERO_SIZED);
}
Expand Down Expand Up @@ -672,16 +672,16 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
}
let def = v.tcx.def_map.borrow().get(&callee.id).map(|d| d.full_def());
let is_const = match def {
Some(def::DefStruct(..)) => true,
Some(def::DefVariant(..)) => {
Some(Def::Struct(..)) => true,
Some(Def::Variant(..)) => {
// Count the discriminator.
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
true
}
Some(def::DefFn(did, _)) => {
Some(Def::Fn(did)) => {
v.handle_const_fn_call(e, did, node_ty)
}
Some(def::DefMethod(did)) => {
Some(Def::Method(did)) => {
match v.tcx.impl_or_trait_item(did).container() {
ty::ImplContainer(_) => {
v.handle_const_fn_call(e, did, node_ty)
Expand Down
30 changes: 15 additions & 15 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchCheckCtxt, pat: &Pat)
let pat_ty = cx.tcx.pat_ty(p);
if let ty::TyEnum(edef, _) = pat_ty.sty {
let def = cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def());
if let Some(DefLocal(..)) = def {
if let Some(Def::Local(..)) = def {
if edef.variants.iter().any(|variant|
variant.name == ident.node.unhygienic_name
&& variant.kind() == VariantKind::Unit
Expand Down Expand Up @@ -454,8 +454,8 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
hir::PatIdent(..) | hir::PatEnum(..) | hir::PatQPath(..) => {
let def = self.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def());
match def {
Some(DefAssociatedConst(did)) |
Some(DefConst(did)) => match lookup_const_by_id(self.tcx, did,
Some(Def::AssociatedConst(did)) |
Some(Def::Const(did)) => match lookup_const_by_id(self.tcx, did,
Some(pat.id), None) {
Some(const_expr) => {
const_expr_to_pat(self.tcx, const_expr, pat.span).map(|new_pat| {
Expand Down Expand Up @@ -757,30 +757,30 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
match pat.node {
hir::PatIdent(..) =>
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
Some(DefStruct(_)) => vec!(Single),
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
Some(Def::Struct(..)) => vec!(Single),
Some(Def::Variant(_, id)) => vec!(Variant(id)),
_ => vec!()
},
hir::PatEnum(..) =>
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
Some(Def::Variant(_, id)) => vec!(Variant(id)),
_ => vec!(Single)
},
hir::PatQPath(..) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
hir::PatStruct(..) =>
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
Some(Def::Variant(_, id)) => vec!(Variant(id)),
_ => vec!(Single)
},
hir::PatLit(ref expr) =>
Expand Down Expand Up @@ -869,10 +869,10 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
hir::PatIdent(_, _, _) => {
let opt_def = cx.tcx.def_map.borrow().get(&pat_id).map(|d| d.full_def());
match opt_def {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
been rewritten"),
Some(DefVariant(_, id, _)) => if *constructor == Variant(id) {
Some(Def::Variant(_, id)) => if *constructor == Variant(id) {
Some(vec!())
} else {
None
Expand All @@ -884,11 +884,11 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
hir::PatEnum(_, ref args) => {
let def = cx.tcx.def_map.borrow().get(&pat_id).unwrap().full_def();
match def {
DefConst(..) | DefAssociatedConst(..) =>
Def::Const(..) | Def::AssociatedConst(..) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
been rewritten"),
DefVariant(_, id, _) if *constructor != Variant(id) => None,
DefVariant(..) | DefStruct(..) => {
Def::Variant(_, id) if *constructor != Variant(id) => None,
Def::Variant(..) | Def::Struct(..) => {
Some(match args {
&Some(ref args) => args.iter().map(|p| &**p).collect(),
&None => vec![DUMMY_WILD_PAT; arity],
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/check_static_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use front::map as ast_map;
use session::Session;
use middle::def::{DefStatic, DefConst, DefAssociatedConst, DefVariant, DefMap};
use middle::def::{Def, DefMap};
use util::nodemap::NodeMap;

use syntax::{ast};
Expand Down Expand Up @@ -238,9 +238,9 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
match e.node {
hir::ExprPath(..) => {
match self.def_map.get(&e.id).map(|d| d.base_def) {
Some(DefStatic(def_id, _)) |
Some(DefAssociatedConst(def_id)) |
Some(DefConst(def_id)) => {
Some(Def::Static(def_id, _)) |
Some(Def::AssociatedConst(def_id)) |
Some(Def::Const(def_id)) => {
if let Some(node_id) = self.ast_map.as_local_node_id(def_id) {
match self.ast_map.get(node_id) {
ast_map::NodeItem(item) =>
Expand All @@ -263,7 +263,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
// affect the specific variant used, but we need to check
// the whole enum definition to see what expression that
// might be (if any).
Some(DefVariant(enum_id, variant_id, false)) => {
Some(Def::Variant(enum_id, variant_id)) => {
if let Some(enum_node_id) = self.ast_map.as_local_node_id(enum_id) {
if let hir::ItemEnum(ref enum_def, ref generics) =
self.ast_map.expect_item(enum_node_id).node
Expand All @@ -276,7 +276,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
} else {
self.sess.span_bug(e.span,
"`check_static_recursion` found \
non-enum in DefVariant");
non-enum in Def::Variant");
}
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use self::EvalHint::*;
use front::map as ast_map;
use front::map::blocks::FnLikeNode;
use middle::cstore::{self, CrateStore, InlinedItem};
use middle::{def, infer, subst, traits};
use middle::{infer, subst, traits};
use middle::def::Def;
use middle::subst::Subst;
use middle::def_id::DefId;
use middle::pat_util::def_to_path;
Expand Down Expand Up @@ -331,9 +332,9 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<hir::Pat>
entry.insert(def);
}
let path = match def.full_def() {
def::DefStruct(def_id) => def_to_path(tcx, def_id),
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
def::DefFn(..) => return P(hir::Pat {
Def::Struct(def_id) => def_to_path(tcx, def_id),
Def::Variant(_, variant_did) => def_to_path(tcx, variant_did),
Def::Fn(..) => return P(hir::Pat {
id: expr.id,
node: hir::PatLit(P(expr.clone())),
span: span,
Expand Down Expand Up @@ -364,12 +365,12 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<hir::Pat>
hir::ExprPath(_, ref path) => {
let opt_def = tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def());
match opt_def {
Some(def::DefStruct(..)) =>
Some(Def::Struct(..)) =>
hir::PatStruct(path.clone(), hir::HirVec::new(), false),
Some(def::DefVariant(..)) =>
Some(Def::Variant(..)) =>
hir::PatEnum(path.clone(), None),
Some(def::DefConst(def_id)) |
Some(def::DefAssociatedConst(def_id)) => {
Some(Def::Const(def_id)) |
Some(Def::AssociatedConst(def_id)) => {
let expr = lookup_const_by_id(tcx, def_id, Some(expr.id), None).unwrap();
return const_expr_to_pat(tcx, expr, span);
},
Expand Down Expand Up @@ -1002,7 +1003,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
None
};
let (const_expr, const_ty) = match opt_def {
Some(def::DefConst(def_id)) => {
Some(Def::Const(def_id)) => {
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
match tcx.map.find(node_id) {
Some(ast_map::NodeItem(it)) => match it.node {
Expand All @@ -1017,7 +1018,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
(lookup_const_by_id(tcx, def_id, Some(e.id), None), None)
}
}
Some(def::DefAssociatedConst(def_id)) => {
Some(Def::AssociatedConst(def_id)) => {
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
match tcx.impl_or_trait_item(def_id).container() {
ty::TraitContainer(trait_id) => match tcx.map.find(node_id) {
Expand Down Expand Up @@ -1052,21 +1053,21 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
(lookup_const_by_id(tcx, def_id, Some(e.id), None), None)
}
}
Some(def::DefVariant(enum_def, variant_def, _)) => {
Some(Def::Variant(enum_def, variant_def)) => {
(lookup_variant_by_id(tcx, enum_def, variant_def), None)
}
Some(def::DefStruct(_)) => {
Some(Def::Struct(..)) => {
return Ok(ConstVal::Struct(e.id))
}
Some(def::DefLocal(_, id)) => {
debug!("DefLocal({:?}): {:?}", id, fn_args);
Some(Def::Local(_, id)) => {
debug!("Def::Local({:?}): {:?}", id, fn_args);
if let Some(val) = fn_args.and_then(|args| args.get(&id)) {
return Ok(val.clone());
} else {
(None, None)
}
},
Some(def::DefMethod(id)) | Some(def::DefFn(id, _)) => return Ok(Function(id)),
Some(Def::Method(id)) | Some(Def::Fn(id)) => return Ok(Function(id)),
_ => (None, None)
};
let const_expr = match const_expr {
Expand Down
11 changes: 8 additions & 3 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

use back::svh::Svh;
use front::map as hir_map;
use middle::def;
use middle::def::{self, Def};
use middle::lang_items;
use middle::ty::{self, Ty};
use middle::ty::{self, Ty, VariantKind};
use middle::def_id::{DefId, DefIndex};
use mir::repr::Mir;
use session::Session;
Expand Down Expand Up @@ -84,7 +84,7 @@ enum_from_u32! {
// Something that a name can resolve to.
#[derive(Copy, Clone, Debug)]
pub enum DefLike {
DlDef(def::Def),
DlDef(Def),
DlImpl(DefId),
DlField
}
Expand Down Expand Up @@ -211,6 +211,8 @@ pub trait CrateStore<'tcx> : Any {

// resolve
fn def_path(&self, def: DefId) -> hir_map::DefPath;
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind>;
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<ChildItem>;
Expand Down Expand Up @@ -380,6 +382,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {

// resolve
fn def_path(&self, def: DefId) -> hir_map::DefPath { unimplemented!() }
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind> { unimplemented!() }
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{ unimplemented!() }
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
{ unimplemented!() }
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { unimplemented!() }
Expand Down
11 changes: 6 additions & 5 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use front::map as ast_map;
use rustc_front::hir;
use rustc_front::intravisit::{self, Visitor};

use middle::{def, pat_util, privacy, ty};
use middle::{pat_util, privacy, ty};
use middle::def::Def;
use middle::def_id::{DefId};
use lint;

Expand Down Expand Up @@ -94,13 +95,13 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {

self.tcx.def_map.borrow().get(id).map(|def| {
match def.full_def() {
def::DefConst(_) | def::DefAssociatedConst(..) => {
Def::Const(_) | Def::AssociatedConst(..) => {
self.check_def_id(def.def_id());
}
_ if self.ignore_non_const_paths => (),
def::DefPrimTy(_) => (),
def::DefSelfTy(..) => (),
def::DefVariant(enum_id, variant_id, _) => {
Def::PrimTy(_) => (),
Def::SelfTy(..) => (),
Def::Variant(enum_id, variant_id) => {
self.check_def_id(enum_id);
if !self.ignore_variant_stack.contains(&variant_id) {
self.check_def_id(variant_id);
Expand Down
Loading

0 comments on commit 51108b6

Please sign in to comment.