From ae8ba88424652a0d263f193cd9c31e024a000546 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Sat, 29 Nov 2014 17:09:12 +1300 Subject: [PATCH] Mostly non-behaviour-changing changes (style, etc.) --- src/librustc/metadata/decoder.rs | 2 +- src/librustc/middle/resolve.rs | 74 ++++++++++++----------- src/librustc/middle/subst.rs | 2 +- src/librustc/middle/traits/util.rs | 2 +- src/librustc/middle/ty.rs | 2 +- src/librustc_typeck/astconv.rs | 19 +----- src/librustc_typeck/check/method/probe.rs | 4 +- src/librustc_typeck/check/mod.rs | 1 + src/librustc_typeck/collect.rs | 67 ++++++++++---------- 9 files changed, 82 insertions(+), 91 deletions(-) diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 0d51e044de9..4acda5bf659 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -435,7 +435,7 @@ pub fn get_symbol(data: &[u8], id: ast::NodeId) -> String { } // Something that a name can resolve to. -#[deriving(Clone)] +#[deriving(Clone,Show)] pub enum DefLike { DlDef(def::Def), DlImpl(ast::DefId), diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index e36fefe578d..59bda245f92 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -108,7 +108,7 @@ pub type ExportMap2 = NodeMap>; pub struct Export2 { pub name: String, // The name of the target. - pub def_id: DefId, // The definition of the target. + pub def_id: DefId, // The definition of the target. } // This set contains all exported definitions from external crates. The set does @@ -314,7 +314,7 @@ impl<'a> Copy for TypeParameters<'a> {} // The rib kind controls the translation of local // definitions (`DefLocal`) to upvars (`DefUpvar`). - +#[deriving(Show)] enum RibKind { // No translation needs to be applied. NormalRibKind, @@ -340,6 +340,7 @@ enum RibKind { impl Copy for RibKind {} // Methods can be required or provided. RequiredMethod methods only occur in traits. +#[deriving(Show)] enum MethodSort { RequiredMethod, ProvidedMethod(NodeId) @@ -414,6 +415,7 @@ enum DuplicateCheckingMode { impl Copy for DuplicateCheckingMode {} /// One local scope. +#[deriving(Show)] struct Rib { bindings: HashMap, kind: RibKind, @@ -728,8 +730,11 @@ impl NameBindings { let type_def = self.type_def.borrow().clone(); match type_def { None => { - let module = Module::new(parent_link, def_id, kind, - external, is_public); + let module = Module::new(parent_link, + def_id, + kind, + external, + is_public); *self.type_def.borrow_mut() = Some(TypeNsDef { modifiers: modifiers, module_def: Some(Rc::new(module)), @@ -774,9 +779,9 @@ impl NameBindings { } Some(type_def) => { *self.type_def.borrow_mut() = Some(TypeNsDef { + module_def: type_def.module_def, type_def: Some(def), type_span: Some(sp), - module_def: type_def.module_def, modifiers: modifiers, }); } @@ -1286,7 +1291,7 @@ impl<'a> Resolver<'a> { } fn get_parent_link(&mut self, parent: ReducedGraphParent, name: Name) - -> ParentLink { + -> ParentLink { match parent { ModuleReducedGraphParent(module_) => { return ModuleParentLink(module_.downgrade(), name); @@ -1578,14 +1583,14 @@ impl<'a> Resolver<'a> { ItemImpl(_, Some(_), _, _) => parent, - ItemTrait(_, _, _, ref methods) => { + ItemTrait(_, _, _, ref items) => { let name_bindings = self.add_child(name, parent.clone(), ForbidDuplicateTypesAndModules, sp); - // Add all the methods within to a new module. + // Add all the items within to a new module. let parent_link = self.get_parent_link(parent.clone(), name); name_bindings.define_module(parent_link, Some(local_def(item.id)), @@ -1598,13 +1603,12 @@ impl<'a> Resolver<'a> { let def_id = local_def(item.id); - // Add the names of all the methods to the trait info. - for method in methods.iter() { - let (name, kind) = match *method { + // Add the names of all the items to the trait info. + for trait_item in items.iter() { + let (name, kind) = match *trait_item { ast::RequiredMethod(_) | ast::ProvidedMethod(_) => { - let ty_m = - ast_util::trait_item_to_ty_method(method); + let ty_m = ast_util::trait_item_to_ty_method(trait_item); let name = ty_m.ident.name; @@ -3353,7 +3357,7 @@ impl<'a> Resolver<'a> { use_lexical_scope: UseLexicalScopeFlag, span: Span, name_search_type: NameSearchType) - -> ResolveResult<(Rc, LastPrivate)> { + -> ResolveResult<(Rc, LastPrivate)> { let module_path_len = module_path.len(); assert!(module_path_len > 0); @@ -3382,7 +3386,9 @@ impl<'a> Resolver<'a> { mpath.slice_to(idx - 1)); return Failed(Some((span, msg))); }, - None => return Failed(None), + None => { + return Failed(None) + } } } Failed(err) => return Failed(err), @@ -3575,8 +3581,7 @@ impl<'a> Resolver<'a> { -> ResolveResult> { // If this module is an anonymous module, resolve the item in the // lexical scope. Otherwise, resolve the item from the crate root. - let resolve_result = self.resolve_item_in_lexical_scope( - module_, name, TypeNS); + let resolve_result = self.resolve_item_in_lexical_scope(module_, name, TypeNS); match resolve_result { Success((target, _)) => { let bindings = &*target.bindings; @@ -5327,15 +5332,15 @@ impl<'a> Resolver<'a> { // resolve a single identifier (used as a varref) fn resolve_identifier(&mut self, - identifier: Ident, - namespace: Namespace, - check_ribs: bool, - span: Span) - -> Option<(Def, LastPrivate)> { + identifier: Ident, + namespace: Namespace, + check_ribs: bool, + span: Span) + -> Option<(Def, LastPrivate)> { if check_ribs { match self.resolve_identifier_in_local_ribs(identifier, - namespace, - span) { + namespace, + span) { Some(def) => { return Some((def, LastMod(AllPublic))); } @@ -5353,7 +5358,7 @@ impl<'a> Resolver<'a> { containing_module: Rc, name: Name, namespace: Namespace) - -> NameDefinition { + -> NameDefinition { // First, search children. self.populate_module_if_necessary(&containing_module); @@ -5423,9 +5428,9 @@ impl<'a> Resolver<'a> { // resolve a "module-relative" path, e.g. a::b::c fn resolve_module_relative_path(&mut self, - path: &Path, - namespace: Namespace) - -> Option<(Def, LastPrivate)> { + path: &Path, + namespace: Namespace) + -> Option<(Def, LastPrivate)> { let module_path = path.segments.init().iter() .map(|ps| ps.identifier.name) .collect::>(); @@ -5442,9 +5447,8 @@ impl<'a> Resolver<'a> { let (span, msg) = match err { Some((span, msg)) => (span, msg), None => { - let msg = format!("Use of undeclared module `{}`", - self.names_to_string( - module_path.as_slice())); + let msg = format!("Use of undeclared type or module `{}`", + self.names_to_string(module_path.as_slice())); (path.span, msg) } }; @@ -5538,10 +5542,10 @@ impl<'a> Resolver<'a> { } fn resolve_identifier_in_local_ribs(&mut self, - ident: Ident, - namespace: Namespace, - span: Span) - -> Option { + ident: Ident, + namespace: Namespace, + span: Span) + -> Option { // Check the local set of ribs. let search_result = match namespace { ValueNS => { diff --git a/src/librustc/middle/subst.rs b/src/librustc/middle/subst.rs index fccb45f8724..d3b1c2d2afc 100644 --- a/src/librustc/middle/subst.rs +++ b/src/librustc/middle/subst.rs @@ -112,7 +112,7 @@ impl<'tcx> Substs<'tcx> { } } -pub fn self_ty(&self) -> Option> { + pub fn self_ty(&self) -> Option> { self.types.get_self().map(|&t| t) } diff --git a/src/librustc/middle/traits/util.rs b/src/librustc/middle/traits/util.rs index 66716267135..159b6961782 100644 --- a/src/librustc/middle/traits/util.rs +++ b/src/librustc/middle/traits/util.rs @@ -139,7 +139,7 @@ impl<'cx, 'tcx> Iterator>> for Supertraits<'cx, 'tcx> { } // determine the `self` type, using fresh variables for all variables -// declared on the impl declaration e.g., `impl for ~[(A,B)]` +// declared on the impl declaration e.g., `impl for Box<[(A,B)]>` // would return ($0, $1) where $0 and $1 are freshly instantiated type // variables. pub fn fresh_substs_for_impl<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>, diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 4c4b5d07f50..3f555ec5c4c 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -1613,7 +1613,7 @@ pub struct RegionParameterDef { pub bounds: Vec, } -/// Information about the type/lifetime parameters associated with an +/// Information about the formal type/lifetime parameters associated with an /// item or method. Analogous to ast::Generics. #[deriving(Clone, Show)] pub struct Generics<'tcx> { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 89e10270f01..b65dbff2f61 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -522,16 +522,6 @@ fn convert_parenthesized_parameters<'tcx,AC>(this: &AC, vec![input_ty, output] } -pub fn instantiate_poly_trait_ref<'tcx,AC,RS>( - this: &AC, - rscope: &RS, - ast_trait_ref: &ast::PolyTraitRef, - self_ty: Option>) - -> Rc> - where AC: AstConv<'tcx>, RS: RegionScope -{ - instantiate_trait_ref(this, rscope, &ast_trait_ref.trait_ref, self_ty) -} /// Instantiates the path for the given trait reference, assuming that it's bound to a valid trait /// type. Returns the def_id for the defining trait. Fails if the type is a type other than a trait @@ -637,7 +627,6 @@ pub fn ast_path_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( rscope, did, &generics, - None, path); let ty = decl_ty.subst(tcx, &substs); TypeAndSubsts { substs: substs, ty: ty } @@ -678,7 +667,7 @@ pub fn ast_path_to_ty_relaxed<'tcx,AC,RS>( Substs::new(VecPerParamSpace::params_from_type(type_params), VecPerParamSpace::params_from_type(region_params)) } else { - ast_path_substs_for_ty(this, rscope, did, &generics, None, path) + ast_path_substs_for_ty(this, rscope, did, &generics, path) }; let ty = decl_ty.subst(tcx, &substs); @@ -777,7 +766,7 @@ fn ast_ty_to_trait_ref<'tcx,AC,RS>(this: &AC, } } _ => { - span_err!(this.tcx().sess, ty.span, E0171, + span_err!(this.tcx().sess, ty.span, E0178, "expected a path on the left-hand side of `+`, not `{}`", pprust::ty_to_string(ty)); match ty.node { @@ -788,8 +777,7 @@ fn ast_ty_to_trait_ref<'tcx,AC,RS>(this: &AC, pprust::ty_to_string(&*mut_ty.ty), pprust::bounds_to_string(bounds)); } - - ast::TyRptr(Some(ref lt), ref mut_ty) => { + ast::TyRptr(Some(ref lt), ref mut_ty) => { span_note!(this.tcx().sess, ty.span, "perhaps you meant `&{} {}({} +{})`? (per RFC 248)", pprust::lifetime_to_string(lt), @@ -806,7 +794,6 @@ fn ast_ty_to_trait_ref<'tcx,AC,RS>(this: &AC, Err(ErrorReported) } } - } fn trait_ref_to_object_type<'tcx,AC,RS>(this: &AC, diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 3fa1234ee6e..cc60b296267 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -1020,7 +1020,7 @@ fn get_method_index<'tcx>(tcx: &ty::ctxt<'tcx>, // iterating down the supertraits of the object's trait until // we find the trait the method came from, counting up the // methods from them. - let mut method_count = 0; + let mut method_count = n_method; ty::each_bound_trait_and_supertraits(tcx, &[subtrait], |bound_ref| { if bound_ref.def_id == trait_ref.def_id { false @@ -1035,7 +1035,7 @@ fn get_method_index<'tcx>(tcx: &ty::ctxt<'tcx>, true } }); - method_count + n_method + method_count } impl<'tcx> Candidate<'tcx> { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 6a7f6bd0ea1..80ea8d90268 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -5355,6 +5355,7 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, region_count, data.lifetimes.len()); substs.mut_regions().truncate(space, 0); + break; } } } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 32892aa94ee..0830bd476a2 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -663,48 +663,43 @@ fn is_associated_type_valid_for_param(ty: Ty, fn find_associated_type_in_generics<'tcx>(tcx: &ty::ctxt<'tcx>, span: Span, - ty: Option>, + self_ty: Option>, associated_type_id: ast::DefId, generics: &ty::Generics<'tcx>) - -> Ty<'tcx> + -> Option> { debug!("find_associated_type_in_generics(ty={}, associated_type_id={}, generics={}", - ty.repr(tcx), associated_type_id.repr(tcx), generics.repr(tcx)); + self_ty.repr(tcx), associated_type_id.repr(tcx), generics.repr(tcx)); - let ty = match ty { + let self_ty = match self_ty { None => { - tcx.sess.span_bug(span, - "find_associated_type_in_generics(): no self \ - type") + return None; } Some(ty) => ty, }; - match ty.sty { + match self_ty.sty { ty::ty_param(ref param_ty) => { - /*let type_parameter = generics.types.get(param_ty.space, - param_ty.idx); - let param_id = type_parameter.def_id;*/ let param_id = param_ty.def_id; for type_parameter in generics.types.iter() { if type_parameter.def_id == associated_type_id && type_parameter.associated_with == Some(param_id) { - return ty::mk_param_from_def(tcx, type_parameter); + return Some(ty::mk_param_from_def(tcx, type_parameter)); } } tcx.sess.span_err( span, format!("no suitable bound on `{}`", - ty.user_string(tcx))[]); - ty::mk_err() + self_ty.user_string(tcx))[]); + Some(ty::mk_err()) } _ => { tcx.sess.span_err( span, "it is currently unsupported to access associated types except \ through a type parameter; this restriction will be lifted in time"); - ty::mk_err() + Some(ty::mk_err()) } } } @@ -762,16 +757,16 @@ impl<'a,'tcx> AstConv<'tcx> for ImplCtxt<'a,'tcx> { fn associated_type_binding(&self, span: Span, - ty: Option>, + self_ty: Option>, trait_id: ast::DefId, associated_type_id: ast::DefId) - -> Ty<'tcx> + -> Option> { - let trait_def = ty::lookup_trait_def(self.tcx(), trait_id); match self.opt_trait_ref_id { + // It's an associated type on the trait that we're + // implementing. Some(trait_ref_id) if trait_ref_id == trait_id => { - // It's an associated type on the trait that we're - // implementing. + let trait_def = ty::lookup_trait_def(self.tcx(), trait_id); assert!(trait_def.generics.types .get_slice(subst::AssocSpace) .iter() @@ -801,7 +796,7 @@ impl<'a,'tcx> AstConv<'tcx> for ImplCtxt<'a,'tcx> { // our bounds. find_associated_type_in_generics(self.ccx.tcx, span, - ty, + self_ty, associated_type_id, self.impl_generics) } @@ -840,17 +835,17 @@ impl<'a,'tcx> AstConv<'tcx> for FnCtxt<'a,'tcx> { fn associated_type_binding(&self, span: Span, - ty: Option>, + self_ty: Option>, _: ast::DefId, associated_type_id: ast::DefId) - -> Ty<'tcx> { + -> Option> { debug!("collect::FnCtxt::associated_type_binding()"); // The ID should map to an associated type on one of the traits in // our bounds. find_associated_type_in_generics(self.ccx.tcx, span, - ty, + self_ty, associated_type_id, self.generics) } @@ -887,17 +882,17 @@ impl<'a,'tcx> AstConv<'tcx> for ImplMethodCtxt<'a,'tcx> { fn associated_type_binding(&self, span: Span, - ty: Option>, + self_ty: Option>, _: ast::DefId, associated_type_id: ast::DefId) - -> Ty<'tcx> { + -> Option> { debug!("collect::ImplMethodCtxt::associated_type_binding()"); // The ID should map to an associated type on one of the traits in // our bounds. find_associated_type_in_generics(self.ccx.tcx, span, - ty, + self_ty, associated_type_id, self.method_generics) } @@ -979,7 +974,7 @@ impl<'a,'tcx> AstConv<'tcx> for TraitMethodCtxt<'a,'tcx> { // our bounds. find_associated_type_in_generics(self.ccx.tcx, span, - ty, + self_ty, associated_type_id, self.method_generics) } @@ -1020,17 +1015,17 @@ impl<'a,'tcx,AC:AstConv<'tcx>> AstConv<'tcx> for GenericsCtxt<'a,'tcx,AC> { fn associated_type_binding(&self, span: Span, - ty: Option>, + self_ty: Option>, _: ast::DefId, associated_type_id: ast::DefId) - -> Ty<'tcx> { + -> Option> { debug!("collect::GenericsCtxt::associated_type_binding()"); // The ID should map to an associated type on one of the traits in // our bounds. find_associated_type_in_generics(self.chain.tcx(), span, - ty, + self_ty, associated_type_id, self.associated_types_generics) } @@ -1364,8 +1359,12 @@ pub fn trait_def_of_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, let self_param_ty = ty::ParamTy::for_self(def_id); - let bounds = compute_bounds(ccx, token::SELF_KEYWORD_NAME, self_param_ty, - bounds.as_slice(), unbound, it.span, + let bounds = compute_bounds(ccx, + token::SELF_KEYWORD_NAME, + self_param_ty, + bounds.as_slice(), + unbound, + it.span, &generics.where_clause); let substs = mk_item_substs(ccx, &ty_generics); @@ -1791,7 +1790,7 @@ fn ty_generics<'tcx,AC>(this: &AC, return result; - fn create_type_parameters_for_associated_types<'tcx,AC>( + fn create_type_parameters_for_associated_types<'tcx, AC>( this: &AC, space: subst::ParamSpace, types: &[ast::TyParam],