rustc_resolve: use the visitor model more, remove redundant repeated lookups.

This commit is contained in:
Eduard Burtescu 2015-02-05 09:19:07 +02:00
parent ffb8092ccf
commit 5809f8ae74
26 changed files with 334 additions and 721 deletions

View File

@ -35,55 +35,36 @@ pub fn check_path_args(tcx: &ty::ctxt,
if (flags & NO_REGIONS) != 0 {
if path.segments.iter().any(|s| s.parameters.has_lifetimes()) {
span_err!(tcx.sess, path.span, E0110,
"region parameters are not allowed on this type");
"lifetime parameters are not allowed on this type");
}
}
}
pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
-> Option<Ty<'tcx>> {
match ast_ty.node {
ast::TyPath(ref path) => {
let a_def = match tcx.def_map.borrow().get(&ast_ty.id) {
None => {
tcx.sess.span_bug(ast_ty.span,
&format!("unbound path {}",
path.repr(tcx)))
}
Some(&d) => d
};
match a_def {
def::DefPrimTy(nty) => {
match nty {
ast::TyBool => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(tcx.types.bool)
}
ast::TyChar => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(tcx.types.char)
}
ast::TyInt(it) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(ty::mk_mach_int(tcx, it))
}
ast::TyUint(uit) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(ty::mk_mach_uint(tcx, uit))
}
ast::TyFloat(ft) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(ty::mk_mach_float(tcx, ft))
}
ast::TyStr => {
Some(ty::mk_str(tcx))
}
}
}
_ => None
if let ast::TyPath(ref path) = ast_ty.node {
let def = match tcx.def_map.borrow().get(&ast_ty.id) {
None => {
tcx.sess.span_bug(ast_ty.span,
&format!("unbound path {}", path.repr(tcx)))
}
Some(&d) => d
};
if let def::DefPrimTy(nty) = def {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(match nty {
ast::TyBool => tcx.types.bool,
ast::TyChar => tcx.types.char,
ast::TyInt(it) => ty::mk_mach_int(tcx, it),
ast::TyUint(uit) => ty::mk_mach_uint(tcx, uit),
ast::TyFloat(ft) => ty::mk_mach_float(tcx, ft),
ast::TyStr => ty::mk_str(tcx)
})
} else {
None
}
_ => None
} else {
None
}
}

View File

@ -456,9 +456,6 @@ impl tr for def::Def {
}
def::DefStruct(did) => def::DefStruct(did.tr(dcx)),
def::DefRegion(nid) => def::DefRegion(dcx.tr_id(nid)),
def::DefTyParamBinder(nid) => {
def::DefTyParamBinder(dcx.tr_id(nid))
}
def::DefLabel(nid) => def::DefLabel(dcx.tr_id(nid))
}
}

View File

@ -54,7 +54,6 @@ pub enum Def {
/// - If it's an ExprPath referring to some tuple struct, then DefMap maps
/// it to a def whose id is the StructDef.ctor_id.
DefStruct(ast::DefId),
DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
DefRegion(ast::NodeId),
DefLabel(ast::NodeId),
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
@ -145,7 +144,6 @@ impl Def {
DefSelfTy(id) |
DefUpvar(id, _) |
DefRegion(id) |
DefTyParamBinder(id) |
DefLabel(id) => {
local_def(id)
}

View File

@ -580,7 +580,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
}
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
def::DefTyParam(..) | def::DefTyParamBinder(..) | def::DefRegion(_) |
def::DefTyParam(..) | def::DefRegion(_) |
def::DefLabel(_) | def::DefSelfTy(..) |
def::DefAssociatedTy(..) | def::DefAssociatedPath(..)=> {
Ok(Rc::new(cmt_ {

View File

@ -23,10 +23,8 @@ use Namespace::{TypeNS, ValueNS};
use NameBindings;
use ParentLink::{self, ModuleParentLink, BlockParentLink};
use Resolver;
use RibKind::*;
use Shadowable;
use TypeNsDef;
use TypeParameters::HasTypeParameters;
use self::DuplicateCheckingMode::*;
use self::NamespaceError::*;
@ -34,7 +32,6 @@ use self::NamespaceError::*;
use rustc::metadata::csearch;
use rustc::metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
use rustc::middle::def::*;
use rustc::middle::subst::FnSpace;
use syntax::ast::{Block, Crate};
use syntax::ast::{DeclItem, DefId};
@ -773,12 +770,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
}
/// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item<F>(&mut self,
foreign_item: &ForeignItem,
parent: &Rc<Module>,
f: F) where
F: FnOnce(&mut Resolver),
{
fn build_reduced_graph_for_foreign_item(&mut self,
foreign_item: &ForeignItem,
parent: &Rc<Module>) {
let name = foreign_item.ident.name;
let is_public = foreign_item.vis == ast::Public;
let modifiers = if is_public { PUBLIC } else { DefModifiers::empty() } | IMPORTABLE;
@ -786,25 +780,15 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
self.add_child(name, parent, ForbidDuplicateValues,
foreign_item.span);
match foreign_item.node {
ForeignItemFn(_, ref generics) => {
let def = DefFn(local_def(foreign_item.id), false);
name_bindings.define_value(def, foreign_item.span, modifiers);
self.with_type_parameter_rib(
HasTypeParameters(generics,
FnSpace,
foreign_item.id,
NormalRibKind),
f);
let def = match foreign_item.node {
ForeignItemFn(..) => {
DefFn(local_def(foreign_item.id), false)
}
ForeignItemStatic(_, m) => {
let def = DefStatic(local_def(foreign_item.id), m);
name_bindings.define_value(def, foreign_item.span, modifiers);
f(self.resolver)
DefStatic(local_def(foreign_item.id), m)
}
}
};
name_bindings.define_value(def, foreign_item.span, modifiers);
}
fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &Rc<Module>) -> Rc<Module> {
@ -980,7 +964,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
}
DefLocal(..) | DefPrimTy(..) | DefTyParam(..) |
DefUse(..) | DefUpvar(..) | DefRegion(..) |
DefTyParamBinder(..) | DefLabel(..) | DefSelfTy(..) => {
DefLabel(..) | DefSelfTy(..) => {
panic!("didn't expect `{:?}`", def);
}
}
@ -1241,16 +1225,7 @@ impl<'a, 'b, 'v, 'tcx> Visitor<'v> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
let parent = &self.parent;
self.builder.build_reduced_graph_for_foreign_item(foreign_item,
parent,
|r| {
let mut v = BuildReducedGraphVisitor {
builder: GraphBuilder { resolver: r },
parent: parent.clone()
};
visit::walk_foreign_item(&mut v, foreign_item);
})
self.builder.build_reduced_graph_for_foreign_item(foreign_item, &self.parent);
}
fn visit_block(&mut self, block: &Block) {

File diff suppressed because it is too large Load Diff

View File

@ -250,7 +250,6 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
def::DefSelfTy(_) |
def::DefRegion(_) |
def::DefTyParamBinder(_) |
def::DefLabel(_) |
def::DefStaticMethod(..) |
def::DefTyParam(..) |

View File

@ -209,9 +209,8 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
}
def::DefMod(..) | def::DefForeignMod(..) | def::DefTrait(..) |
def::DefTy(..) | def::DefPrimTy(..) | def::DefAssociatedTy(..) |
def::DefUse(..) | def::DefTyParamBinder(..) |
def::DefRegion(..) | def::DefLabel(..) | def::DefTyParam(..) |
def::DefSelfTy(..) | def::DefAssociatedPath(..) => {
def::DefUse(..) | def::DefRegion(..) | def::DefLabel(..) |
def::DefTyParam(..) | def::DefSelfTy(..) | def::DefAssociatedPath(..) => {
bcx.tcx().sess.span_bug(
ref_expr.span,
&format!("cannot translate def {:?} \

View File

@ -4729,7 +4729,6 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
def::DefSelfTy(..) |
def::DefStruct(..) |
def::DefVariant(..) |
def::DefTyParamBinder(..) |
def::DefTy(..) |
def::DefAssociatedTy(..) |
def::DefAssociatedPath(..) |

View File

@ -1344,8 +1344,6 @@ pub enum Type {
typarams: Option<Vec<TyParamBound>>,
did: ast::DefId,
},
// I have no idea how to usefully use this.
TyParamBinder(ast::NodeId),
/// For parameterized types, so the consumer of the JSON don't go
/// looking for types which don't exist anywhere.
Generic(String),
@ -2416,7 +2414,6 @@ fn resolve_type(cx: &DocContext,
ast::TyFloat(ast::TyF64) => return Primitive(F64),
},
def::DefTyParam(_, _, _, n) => return Generic(token::get_name(n).to_string()),
def::DefTyParamBinder(i) => return TyParamBinder(i),
_ => {}
};
let did = register_def(&*cx, def);

View File

@ -454,9 +454,6 @@ fn tybounds(w: &mut fmt::Formatter,
impl fmt::Display for clean::Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::TyParamBinder(id) => {
f.write_str(&cache().typarams[ast_util::local_def(id)])
}
clean::Generic(ref name) => {
f.write_str(name)
}

View File

@ -70,7 +70,7 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec!(borrowed_self()),
ret_ty: Literal(path!(bool)),
ret_ty: Literal(path_local!(bool)),
attributes: attrs,
combine_substructure: combine_substructure(box |a, b, c| {
$f(a, b, c)

View File

@ -36,7 +36,7 @@ pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec!(borrowed_self()),
ret_ty: Literal(path!(bool)),
ret_ty: Literal(path_local!(bool)),
attributes: attrs,
combine_substructure: combine_substructure(box |cx, span, substr| {
cs_op($op, $equal, cx, span, substr)

View File

@ -30,6 +30,12 @@ macro_rules! path {
)
}
macro_rules! path_local {
($x:ident) => (
::ext::deriving::generic::ty::Path::new_local(stringify!($x))
)
}
macro_rules! pathvec_std {
($cx:expr, $first:ident :: $($rest:ident)::+) => (
if $cx.use_std {

View File

@ -38,7 +38,7 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
name: "from_i64",
generics: LifetimeBounds::empty(),
explicit_self: None,
args: vec!(Literal(path!(i64))),
args: vec!(Literal(path_local!(i64))),
ret_ty: Literal(Path::new_(pathvec_std!(cx, core::option::Option),
None,
vec!(box Self_),
@ -53,7 +53,7 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
name: "from_u64",
generics: LifetimeBounds::empty(),
explicit_self: None,
args: vec!(Literal(path!(u64))),
args: vec!(Literal(path_local!(u64))),
ret_ty: Literal(Path::new_(pathvec_std!(cx, core::option::Option),
None,
vec!(box Self_),

View File

@ -24,7 +24,7 @@ extern "rust-intrinsic" {
// Unresolved bounds should still error.
fn align_of<T: NoSuchTrait>() -> usize;
//~^ ERROR attempt to bound type parameter with a nonexistent trait `NoSuchTrait`
//~^ ERROR use of undeclared trait name `NoSuchTrait`
}
fn main() {}

View File

@ -36,9 +36,6 @@ fn main() {
import(); //~ ERROR: unresolved
foo::<A>(); //~ ERROR: undeclared
//~^ ERROR: undeclared
foo::<C>(); //~ ERROR: undeclared
//~^ ERROR: undeclared
foo::<D>(); //~ ERROR: undeclared
//~^ ERROR: undeclared
}

View File

@ -14,7 +14,8 @@ enum Bar<T> { What }
fn foo<T>() {
static a: Bar<T> = Bar::What;
//~^ ERROR: cannot use an outer type parameter in this context
//~^ ERROR cannot use an outer type parameter in this context
//~| ERROR use of undeclared type name `T`
}
fn main() {

View File

@ -11,7 +11,9 @@
fn main() {
let foo = 100;
static y: isize = foo + 1; //~ ERROR: attempt to use a non-constant value in a constant
static y: isize = foo + 1;
//~^ ERROR attempt to use a non-constant value in a constant
//~| ERROR unresolved name `foo`
println!("{}", y);
}

View File

@ -13,7 +13,9 @@ fn main() {
#[derive(Debug)]
enum Stuff {
Bar = foo //~ ERROR attempt to use a non-constant value in a constant
Bar = foo
//~^ ERROR attempt to use a non-constant value in a constant
//~| ERROR unresolved name `foo`
}
println!("{}", Stuff::Bar);

View File

@ -9,7 +9,9 @@
// except according to those terms.
fn f(x:isize) {
static child: isize = x + 1; //~ ERROR attempt to use a non-constant value in a constant
static child: isize = x + 1;
//~^ ERROR attempt to use a non-constant value in a constant
//~| ERROR unresolved name `x`
}
fn main() {}

View File

@ -17,6 +17,7 @@ impl PTrait for P {
fn getChildOption(&self) -> Option<Box<P>> {
static childVal: Box<P> = self.child.get();
//~^ ERROR attempt to use a non-constant value in a constant
//~| ERROR unresolved name `self`
panic!();
}
}

View File

@ -18,11 +18,11 @@
mod foo {
mod baz {
struct Test;
impl Add for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Clone for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Iterator for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl ToString for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Writer for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Add for Test {} //~ ERROR: use of undeclared trait
impl Clone for Test {} //~ ERROR: use of undeclared trait
impl Iterator for Test {} //~ ERROR: use of undeclared trait
impl ToString for Test {} //~ ERROR: use of undeclared trait
impl Writer for Test {} //~ ERROR: use of undeclared trait
fn foo() {
drop(2) //~ ERROR: unresolved name
@ -30,11 +30,11 @@ mod foo {
}
struct Test;
impl Add for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Clone for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Iterator for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl ToString for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Writer for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Add for Test {} //~ ERROR: use of undeclared trait
impl Clone for Test {} //~ ERROR: use of undeclared trait
impl Iterator for Test {} //~ ERROR: use of undeclared trait
impl ToString for Test {} //~ ERROR: use of undeclared trait
impl Writer for Test {} //~ ERROR: use of undeclared trait
fn foo() {
drop(2) //~ ERROR: unresolved name
@ -45,11 +45,11 @@ fn qux() {
#[no_implicit_prelude]
mod qux_inner {
struct Test;
impl Add for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Clone for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Iterator for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl ToString for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Writer for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Add for Test {} //~ ERROR: use of undeclared trait
impl Clone for Test {} //~ ERROR: use of undeclared trait
impl Iterator for Test {} //~ ERROR: use of undeclared trait
impl ToString for Test {} //~ ERROR: use of undeclared trait
impl Writer for Test {} //~ ERROR: use of undeclared trait
fn foo() {
drop(2) //~ ERROR: unresolved name

View File

@ -17,11 +17,11 @@
// fail with the same error message).
struct Test;
impl Add for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Clone for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Iterator for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl ToString for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Writer for Test {} //~ ERROR: attempt to implement a nonexistent trait
impl Add for Test {} //~ ERROR: use of undeclared trait
impl Clone for Test {} //~ ERROR: use of undeclared trait
impl Iterator for Test {} //~ ERROR: use of undeclared trait
impl ToString for Test {} //~ ERROR: use of undeclared trait
impl Writer for Test {} //~ ERROR: use of undeclared trait
fn main() {
drop(2) //~ ERROR: unresolved name

View File

@ -10,11 +10,11 @@
trait NewTrait : SomeNonExistentTrait {}
//~^ ERROR attempt to derive a nonexistent trait `SomeNonExistentTrait`
//~^ ERROR use of undeclared trait name `SomeNonExistentTrait`
impl SomeNonExistentTrait for isize {}
//~^ ERROR attempt to implement a nonexistent trait `SomeNonExistentTrait`
//~^ ERROR use of undeclared trait name `SomeNonExistentTrait`
fn f<T:SomeNonExistentTrait>() {}
//~^ ERROR attempt to bound type parameter with a nonexistent trait `SomeNonExistentTrait`
//~^ ERROR use of undeclared trait name `SomeNonExistentTrait`

View File

@ -10,7 +10,7 @@
#![feature(unboxed_closures)]
fn f<F:Nonexist(isize) -> isize>(x: F) {} //~ ERROR nonexistent trait `Nonexist`
fn f<F:Nonexist(isize) -> isize>(x: F) {} //~ ERROR undeclared trait name `Nonexist`
type Typedef = isize;