From 9ed9d6d0d09a690e81841f1fc2e02f16bc9ee2c5 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 3 Nov 2018 00:07:56 +0300 Subject: [PATCH 01/26] resolve: Filter away macro prelude in modules with `#[no_implicit_prelude]` on 2018 edition --- src/librustc/session/mod.rs | 4 ++++ src/librustc_resolve/macros.rs | 12 ++++++++---- src/test/ui/hygiene/no_implicit_prelude-2018.rs | 11 +++++++++++ src/test/ui/hygiene/no_implicit_prelude-2018.stderr | 10 ++++++++++ src/test/ui/hygiene/no_implicit_prelude.rs | 5 ++++- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/hygiene/no_implicit_prelude-2018.rs create mode 100644 src/test/ui/hygiene/no_implicit_prelude-2018.stderr diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index a17825a877d88..d5513080daf5f 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -963,6 +963,10 @@ impl Session { self.opts.debugging_opts.teach && self.diagnostic().must_teach(code) } + pub fn rust_2015(&self) -> bool { + self.opts.edition == Edition::Edition2015 + } + /// Are we allowed to use features from the Rust 2018 edition? pub fn rust_2018(&self) -> bool { self.opts.edition >= Edition::Edition2018 diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index d5f344346c2d1..83c32a579ccb4 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -660,10 +660,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> { binding.map(|binding| (binding, Flags::MODULE, Flags::empty())) } WhereToResolve::MacroUsePrelude => { - match self.macro_use_prelude.get(&ident.name).cloned() { - Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())), - None => Err(Determinacy::Determined), + let mut result = Err(Determinacy::Determined); + if use_prelude || self.session.rust_2015() { + if let Some(binding) = self.macro_use_prelude.get(&ident.name).cloned() { + result = Ok((binding, Flags::PRELUDE, Flags::empty())); + } } + result } WhereToResolve::BuiltinMacros => { match self.builtin_macros.get(&ident.name).cloned() { @@ -682,7 +685,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> { } } WhereToResolve::LegacyPluginHelpers => { - if self.session.plugin_attributes.borrow().iter() + if (use_prelude || self.session.rust_2015()) && + self.session.plugin_attributes.borrow().iter() .any(|(name, _)| ident.name == &**name) { let binding = (Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper), ty::Visibility::Public, ident.span, Mark::root()) diff --git a/src/test/ui/hygiene/no_implicit_prelude-2018.rs b/src/test/ui/hygiene/no_implicit_prelude-2018.rs new file mode 100644 index 0000000000000..3ad7435fecf29 --- /dev/null +++ b/src/test/ui/hygiene/no_implicit_prelude-2018.rs @@ -0,0 +1,11 @@ +// edition:2018 + +#[no_implicit_prelude] +mod bar { + fn f() { + ::std::print!(""); // OK + print!(); //~ ERROR cannot find macro `print!` in this scope + } +} + +fn main() {} diff --git a/src/test/ui/hygiene/no_implicit_prelude-2018.stderr b/src/test/ui/hygiene/no_implicit_prelude-2018.stderr new file mode 100644 index 0000000000000..370fc9784ad4d --- /dev/null +++ b/src/test/ui/hygiene/no_implicit_prelude-2018.stderr @@ -0,0 +1,10 @@ +error: cannot find macro `print!` in this scope + --> $DIR/no_implicit_prelude-2018.rs:7:9 + | +LL | print!(); //~ ERROR cannot find macro `print!` in this scope + | ^^^^^ + | + = help: have you added the `#[macro_use]` on the module/import? + +error: aborting due to previous error + diff --git a/src/test/ui/hygiene/no_implicit_prelude.rs b/src/test/ui/hygiene/no_implicit_prelude.rs index bf07bc05491cc..5b6041945abea 100644 --- a/src/test/ui/hygiene/no_implicit_prelude.rs +++ b/src/test/ui/hygiene/no_implicit_prelude.rs @@ -21,7 +21,10 @@ mod bar { Vec::new(); //~ ERROR failed to resolve ().clone() //~ ERROR no method named `clone` found } - fn f() { ::foo::m!(); } + fn f() { + ::foo::m!(); + println!(); // OK on 2015 edition (at least for now) + } } fn main() {} From d08a42bf2c2115fc1869b3a72ee40fa4dd445795 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Thu, 1 Nov 2018 19:43:38 +0000 Subject: [PATCH 02/26] Look at projections from supertraits when constructing trait objects. --- src/librustc/hir/mod.rs | 6 +- src/librustc/traits/util.rs | 2 +- src/librustc/ty/mod.rs | 47 +++++++-------- src/librustc_typeck/astconv.rs | 85 ++++++++++++++-------------- src/librustc_typeck/check/wfcheck.rs | 6 +- 5 files changed, 77 insertions(+), 69 deletions(-) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index a2095ff40c040..f57e3ff913b38 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -506,9 +506,9 @@ pub enum TraitBoundModifier { } /// The AST represents all type param bounds as types. -/// typeck::collect::compute_bounds matches these against -/// the "special" built-in traits (see middle::lang_items) and -/// detects Copy, Send and Sync. +/// `typeck::collect::compute_bounds` matches these against +/// the "special" built-in traits (see `middle::lang_items`) and +/// detects `Copy`, `Send` and `Sync`. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub enum GenericBound { Trait(PolyTraitRef, TraitBoundModifier), diff --git a/src/librustc/traits/util.rs b/src/librustc/traits/util.rs index 74f8d67ce0484..24097fcca703b 100644 --- a/src/librustc/traits/util.rs +++ b/src/librustc/traits/util.rs @@ -333,7 +333,7 @@ impl FilterToTraits { } } -impl<'tcx,I:Iterator>> Iterator for FilterToTraits { +impl<'tcx, I: Iterator>> Iterator for FilterToTraits { type Item = ty::PolyTraitRef<'tcx>; fn next(&mut self) -> Option> { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index c7c197d11c03b..c18c55bcb5db7 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -294,7 +294,7 @@ impl Visibility { } } - /// Returns true if an item with this visibility is accessible from the given block. + /// Returns `true` if an item with this visibility is accessible from the given block. pub fn is_accessible_from(self, module: DefId, tree: T) -> bool { let restriction = match self { // Public items are visible everywhere. @@ -309,7 +309,7 @@ impl Visibility { tree.is_descendant_of(module, restriction) } - /// Returns true if this visibility is at least as accessible as the given visibility + /// Returns `true` if this visibility is at least as accessible as the given visibility pub fn is_at_least(self, vis: Visibility, tree: T) -> bool { let vis_restriction = match vis { Visibility::Public => return self == Visibility::Public, @@ -320,7 +320,7 @@ impl Visibility { self.is_accessible_from(vis_restriction, tree) } - // Returns true if this item is visible anywhere in the local crate. + // Returns `true` if this item is visible anywhere in the local crate. pub fn is_visible_locally(self) -> bool { match self { Visibility::Public => true, @@ -451,7 +451,7 @@ bitflags! { // FIXME: Rename this to the actual property since it's used for generators too const HAS_TY_CLOSURE = 1 << 9; - // true if there are "names" of types and regions and so forth + // `true` if there are "names" of types and regions and so forth // that are local to a particular fn const HAS_FREE_LOCAL_NAMES = 1 << 10; @@ -953,7 +953,7 @@ impl<'a, 'gcx, 'tcx> Generics { _ => bug!("expected lifetime parameter, but found another generic parameter") } } else { - tcx.generics_of(self.parent.expect("parent_count>0 but no parent?")) + tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?")) .region_param(param, tcx) } } @@ -970,7 +970,7 @@ impl<'a, 'gcx, 'tcx> Generics { _ => bug!("expected type parameter, but found another generic parameter") } } else { - tcx.generics_of(self.parent.expect("parent_count>0 but no parent?")) + tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?")) .type_param(param, tcx) } } @@ -993,6 +993,7 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> { self.instantiate_into(tcx, &mut instantiated, substs); instantiated } + pub fn instantiate_own(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, substs: &Substs<'tcx>) -> InstantiatedPredicates<'tcx> { InstantiatedPredicates { @@ -1256,14 +1257,14 @@ pub struct ProjectionPredicate<'tcx> { pub type PolyProjectionPredicate<'tcx> = Binder>; impl<'tcx> PolyProjectionPredicate<'tcx> { - /// Returns the def-id of the associated item being projected. + /// Returns the `DefId` of the associated item being projected. pub fn item_def_id(&self) -> DefId { self.skip_binder().projection_ty.item_def_id } pub fn to_poly_trait_ref(&self, tcx: TyCtxt<'_, '_, '_>) -> PolyTraitRef<'tcx> { - // Note: unlike with TraitRef::to_poly_trait_ref(), - // self.0.trait_ref is permitted to have escaping regions. + // Note: unlike with `TraitRef::to_poly_trait_ref()`, + // `self.0.trait_ref` is permitted to have escaping regions. // This is because here `self` has a `Binder` and so does our // return value, so we are preserving the number of binding // levels. @@ -1274,12 +1275,12 @@ impl<'tcx> PolyProjectionPredicate<'tcx> { self.map_bound(|predicate| predicate.ty) } - /// The DefId of the TraitItem for the associated type. + /// The `DefId` of the `TraitItem` for the associated type. /// - /// Note that this is not the DefId of the TraitRef containing this - /// associated type, which is in tcx.associated_item(projection_def_id()).container. + /// Note that this is not the `DefId` of the `TraitRef` containing this + /// associated type, which is in `tcx.associated_item(projection_def_id()).container`. pub fn projection_def_id(&self) -> DefId { - // ok to skip binder since trait def-id does not care about regions + // okay to skip binder since trait def-id does not care about regions self.skip_binder().projection_ty.item_def_id } } @@ -1515,14 +1516,14 @@ impl UniverseIndex { UniverseIndex::from_u32(self.private.checked_add(1).unwrap()) } - /// True if `self` can name a name from `other` -- in other words, + /// `true` if `self` can name a name from `other` -- in other words, /// if the set of names in `self` is a superset of those in /// `other` (`self >= other`). pub fn can_name(self, other: UniverseIndex) -> bool { self.private >= other.private } - /// True if `self` cannot name some names from `other` -- in other + /// `true` if `self` cannot name some names from `other` -- in other /// words, if the set of names in `self` is a strict subset of /// those in `other` (`self < other`). pub fn cannot_name(self, other: UniverseIndex) -> bool { @@ -1574,7 +1575,7 @@ impl<'tcx> ParamEnv<'tcx> { /// are revealed. This is suitable for monomorphized, post-typeck /// environments like codegen or doing optimizations. /// - /// NB. If you want to have predicates in scope, use `ParamEnv::new`, + /// N.B. If you want to have predicates in scope, use `ParamEnv::new`, /// or invoke `param_env.with_reveal_all()`. pub fn reveal_all() -> Self { Self::new(List::empty(), Reveal::All) @@ -1979,14 +1980,14 @@ impl ReprOptions { self.int.unwrap_or(attr::SignedInt(ast::IntTy::Isize)) } - /// Returns true if this `#[repr()]` should inhabit "smart enum + /// Returns `true` if this `#[repr()]` should inhabit "smart enum /// layout" optimizations, such as representing `Foo<&T>` as a /// single pointer. pub fn inhibit_enum_layout_opt(&self) -> bool { self.c() || self.int.is_some() } - /// Returns true if this `#[repr()]` should inhibit struct field reordering + /// Returns `true` if this `#[repr()]` should inhibit struct field reordering /// optimizations, such as with repr(C) or repr(packed(1)). pub fn inhibit_struct_field_reordering_opt(&self) -> bool { !(self.flags & ReprFlags::IS_UNOPTIMISABLE).is_empty() || (self.pack == 1) @@ -2089,7 +2090,7 @@ impl<'a, 'gcx, 'tcx> AdtDef { self.flags.intersects(AdtFlags::IS_FUNDAMENTAL) } - /// Returns true if this is PhantomData. + /// Returns `true` if this is PhantomData. #[inline] pub fn is_phantom_data(&self) -> bool { self.flags.intersects(AdtFlags::IS_PHANTOM_DATA) @@ -2105,7 +2106,7 @@ impl<'a, 'gcx, 'tcx> AdtDef { self.flags.intersects(AdtFlags::IS_RC) } - /// Returns true if this is Box. + /// Returns `true` if this is Box. #[inline] pub fn is_box(&self) -> bool { self.flags.intersects(AdtFlags::IS_BOX) @@ -2422,7 +2423,7 @@ impl<'a, 'tcx> ClosureKind { } } - /// True if this a type that impls this closure kind + /// Returns `true` if this a type that impls this closure kind /// must also implement `other`. pub fn extends(self, other: ty::ClosureKind) -> bool { match (self, other) { @@ -2678,7 +2679,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { as Box + 'a> } - /// Returns true if the impls are the same polarity and the trait either + /// Returns `true` if the impls are the same polarity and the trait either /// has no items or is annotated #[marker] and prevents item overrides. pub fn impls_are_allowed_to_overlap(self, def_id1: DefId, def_id2: DefId) -> bool { if self.features().overlapping_marker_traits { @@ -2802,7 +2803,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { attr::contains_name(&self.get_attrs(did), attr) } - /// Returns true if this is an `auto trait`. + /// Returns `true` if this is an `auto trait`. pub fn trait_is_auto(self, trait_def_id: DefId) -> bool { self.trait_def(trait_def_id).has_auto_impl } diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 18f8473b5b56d..daf04aa0db223 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -451,7 +451,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } // We manually build up the substitution, rather than using convenience - // methods in subst.rs so that we can iterate over the arguments and + // methods in `subst.rs` so that we can iterate over the arguments and // parameters in lock-step linearly, rather than trying to match each pair. let mut substs: SmallVec<[Kind<'tcx>; 8]> = SmallVec::with_capacity(count); @@ -469,7 +469,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } } - // (Unless it's been handled in `parent_substs`) `Self` is handled first. + // `Self` is handled first, unless it's been handled in `parent_substs`. if has_self { if let Some(¶m) = params.peek() { if param.index == 0 { @@ -698,7 +698,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { trait_ref.path.segments.last().unwrap()) } - /// Get the DefId of the given trait ref. It _must_ actually be a trait. + /// Get the `DefId` of the given trait ref. It _must_ actually be a trait. fn trait_def_id(&self, trait_ref: &hir::TraitRef) -> DefId { let path = &trait_ref.path; match path.def { @@ -711,7 +711,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } } - /// The given `trait_ref` must actually be trait. + /// The given trait ref must actually be a trait. pub(super) fn instantiate_poly_trait_ref_inner(&self, trait_ref: &hir::TraitRef, self_ty: Ty<'tcx>, @@ -738,7 +738,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { let predicate: Result<_, ErrorReported> = self.ast_type_binding_to_poly_projection_predicate( trait_ref.ref_id, poly_trait_ref, binding, speculative, &mut dup_bindings); - // ok to ignore Err because ErrorReported (see above) + // okay to ignore Err because of ErrorReported (see above) Some((predicate.ok()?, binding.span)) })); @@ -947,14 +947,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { ) } - /// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by - /// removing the dummy `Self` type (`TRAIT_OBJECT_DUMMY_SELF`). - fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>) - -> ty::ExistentialTraitRef<'tcx> { - assert_eq!(trait_ref.self_ty().sty, TRAIT_OBJECT_DUMMY_SELF); - ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) - } - fn conv_object_ty_poly_trait_ref(&self, span: Span, trait_bounds: &[hir::PolyTraitRef], @@ -969,7 +961,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { return tcx.types.err; } - let mut projection_bounds = vec![]; + let mut projection_bounds = Vec::new(); let dummy_self = tcx.mk_ty(TRAIT_OBJECT_DUMMY_SELF); let principal = self.instantiate_poly_trait_ref(&trait_bounds[0], dummy_self, @@ -994,23 +986,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { .emit(); } - // Erase the dummy_self (TRAIT_OBJECT_DUMMY_SELF) used above. - let existential_principal = principal.map_bound(|trait_ref| { - self.trait_ref_to_existential(trait_ref) - }); - let existential_projections = projection_bounds.iter().map(|(bound, _)| { - bound.map_bound(|b| { - let trait_ref = self.trait_ref_to_existential(b.projection_ty.trait_ref(tcx)); - ty::ExistentialProjection { - ty: b.ty, - item_def_id: b.projection_ty.item_def_id, - substs: trait_ref.substs, - } - }) - }); - // Check that there are no gross object safety violations; - // most importantly, that the supertraits don't contain Self, + // most importantly, that the supertraits don't contain `Self`, // to avoid ICEs. let object_safety_violations = tcx.global_tcx().astconv_object_safety_violations(principal.def_id()); @@ -1021,13 +998,23 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { return tcx.types.err; } - // Use a BTreeSet to keep output in a more consistent order. + // Use a `BTreeSet` to keep output in a more consistent order. let mut associated_types = BTreeSet::default(); for tr in traits::supertraits(tcx, principal) { associated_types.extend(tcx.associated_items(tr.def_id()) .filter(|item| item.kind == ty::AssociatedKind::Type) .map(|item| item.def_id)); + + projection_bounds.extend(tcx.predicates_of(tr.def_id()) + .predicates.into_iter() + .filter_map(|(pred, span)| { + if let ty::Predicate::Projection(proj) = pred { + Some((proj, span)) + } else { + None + } + })); } for (projection_bound, _) in &projection_bounds { @@ -1046,11 +1033,28 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { .emit(); } + // Erase the `dummy_self` (`TRAIT_OBJECT_DUMMY_SELF`) used above. + let existential_principal = principal.map_bound(|trait_ref| { + assert_eq!(trait_ref.self_ty().sty, TRAIT_OBJECT_DUMMY_SELF); + ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) + }); + let existential_projections = projection_bounds.iter().map(|(bound, _)| { + bound.map_bound(|b| { + let trait_ref = ty::ExistentialTraitRef::erase_self_ty(self.tcx(), + b.projection_ty.trait_ref(tcx)); + ty::ExistentialProjection { + ty: b.ty, + item_def_id: b.projection_ty.item_def_id, + substs: trait_ref.substs, + } + }) + }); + // Dedup auto traits so that `dyn Trait + Send + Send` is the same as `dyn Trait + Send`. auto_traits.sort(); auto_traits.dedup(); - // skip_binder is okay, because the predicates are re-bound. + // Calling `skip_binder` is okay, because the predicates are re-bound. let mut v = iter::once(ty::ExistentialPredicate::Trait(*existential_principal.skip_binder())) .chain(auto_traits.into_iter().map(ty::ExistentialPredicate::AutoTrait)) @@ -1128,7 +1132,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { span) } - // Checks that bounds contains exactly one element and reports appropriate // errors otherwise. fn one_bound_for_assoc_type(&self, @@ -1186,11 +1189,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } // Create a type from a path to an associated type. - // For a path A::B::C::D, ty and ty_path_def are the type and def for A::B::C - // and item_segment is the path segment for D. We return a type and a def for + // For a path `A::B::C::D`, `ty` and `ty_path_def` are the type and def for `A::B::C` + // and item_segment is the path segment for `D`. We return a type and a def for // the whole path. - // Will fail except for T::A and Self::A; i.e., if ty/ty_path_def are not a type - // parameter or Self. + // Will fail except for `T::A` and `Self::A`; i.e., if `ty`/`ty_path_def` are not a type + // parameter or `Self`. pub fn associated_path_def_to_ty(&self, ref_id: ast::NodeId, span: Span, @@ -1210,7 +1213,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { // item is declared. let bound = match (&ty.sty, ty_path_def) { (_, Def::SelfTy(Some(_), Some(impl_def_id))) => { - // `Self` in an impl of a trait - we have a concrete self type and a + // `Self` in an impl of a trait - we have a concrete `self` type and a // trait reference. let trait_ref = match tcx.impl_trait_ref(impl_def_id) { Some(trait_ref) => trait_ref, @@ -1361,7 +1364,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { let span = path.span; match path.def { Def::Existential(did) => { - // check for desugared impl trait + // Check for desugared impl trait. assert!(ty::is_impl_trait_defn(tcx, did).is_none()); let item_segment = path.segments.split_last().unwrap(); self.prohibit_generics(item_segment.1); @@ -1398,7 +1401,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { tcx.mk_ty_param(index, tcx.hir.name(node_id).as_interned_str()) } Def::SelfTy(_, Some(def_id)) => { - // Self in impl (we know the concrete type). + // `Self` in impl (we know the concrete type) assert_eq!(opt_self_ty, None); self.prohibit_generics(&path.segments); @@ -1406,7 +1409,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { tcx.at(span).type_of(def_id) } Def::SelfTy(Some(_), None) => { - // Self in trait. + // `Self` in trait assert_eq!(opt_self_ty, None); self.prohibit_generics(&path.segments); tcx.mk_self_type() diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 266b9e3c0abad..53f536578632c 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -186,6 +186,8 @@ fn check_associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId, span: Span, sig_if_method: Option<&hir::MethodSig>) { + debug!("check_associated_item: {:?}", item_id); + let code = ObligationCauseCode::MiscObligation; for_id(tcx, item_id, span).with_fcx(|fcx, tcx| { let item = fcx.tcx.associated_item(fcx.tcx.hir.local_def_id(item_id)); @@ -311,6 +313,8 @@ fn check_type_defn<'a, 'tcx, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } fn check_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) { + debug!("check_trait: {:?}", item.id); + let trait_def_id = tcx.hir.local_def_id(item.id); let trait_def = tcx.trait_def(trait_def_id); @@ -1012,7 +1016,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } None => { - // Inherent impl: take implied bounds from the self type. + // Inherent impl: take implied bounds from the `self` type. let self_ty = self.tcx.type_of(impl_def_id); let self_ty = self.normalize_associated_types_in(span, &self_ty); vec![self_ty] From 0e89f570d224ee08b6e32aa9ea8ea44a4e9244f3 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Sun, 4 Nov 2018 04:47:10 +0000 Subject: [PATCH 03/26] Added tests. --- src/librustc/diagnostics.rs | 4 +- src/librustc/ty/mod.rs | 4 +- src/librustc_typeck/astconv.rs | 42 +++++++++++++++---- src/librustc_typeck/check/wfcheck.rs | 4 +- src/librustc_typeck/collect.rs | 12 +++--- src/librustc_typeck/diagnostics.rs | 1 + src/test/run-pass/issues/issue-24010.rs | 22 ++++++++++ ...-object-type.rs => trait-alias-objects.rs} | 5 +-- src/test/ui/error-codes/E0191.rs | 3 +- src/test/ui/error-codes/E0719.rs | 17 ++++++++ src/test/ui/issue-51947.rs | 17 -------- src/test/ui/issues/issue-51947.rs | 27 ++++++++++++ .../trait-alias-associated-type-rebound.rs | 17 ++++++++ 13 files changed, 133 insertions(+), 42 deletions(-) create mode 100644 src/test/run-pass/issues/issue-24010.rs rename src/test/run-pass/traits/{trait-alias-object-type.rs => trait-alias-objects.rs} (80%) create mode 100644 src/test/ui/error-codes/E0719.rs delete mode 100644 src/test/ui/issue-51947.rs create mode 100644 src/test/ui/issues/issue-51947.rs create mode 100644 src/test/ui/traits/trait-alias-associated-type-rebound.rs diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 1d21a5cf79d10..96590c1fc72d4 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -2134,7 +2134,7 @@ static X: u32 = 42; register_diagnostics! { -// E0006 // merged with E0005 +// E0006, // merged with E0005 // E0101, // replaced with E0282 // E0102, // replaced with E0282 // E0134, @@ -2183,9 +2183,7 @@ register_diagnostics! { E0657, // `impl Trait` can only capture lifetimes bound at the fn level E0687, // in-band lifetimes cannot be used in `fn`/`Fn` syntax E0688, // in-band lifetimes cannot be mixed with explicit lifetime binders - E0697, // closures cannot be static - E0707, // multiple elided lifetimes used in arguments of `async fn` E0708, // `async` non-`move` closures with arguments are not currently supported E0709, // multiple different lifetimes used in arguments of `async fn` diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index c18c55bcb5db7..71d59750d6c87 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1239,11 +1239,11 @@ pub type PolySubtypePredicate<'tcx> = ty::Binder>; /// This kind of predicate has no *direct* correspondent in the /// syntax, but it roughly corresponds to the syntactic forms: /// -/// 1. `T : TraitRef<..., Item=Type>` +/// 1. `T: TraitRef<..., Item=Type>` /// 2. `>::Item == Type` (NYI) /// /// In particular, form #1 is "desugared" to the combination of a -/// normal trait predicate (`T : TraitRef<...>`) and one of these +/// normal trait predicate (`T: TraitRef<...>`) and one of these /// predicates. Form #2 is a broader form in that it also permits /// equality between arbitrary types. Processing an instance of /// Form #2 eventually yields one of these `ProjectionPredicate` diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index daf04aa0db223..3aaa5810eab38 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -831,7 +831,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { let tcx = self.tcx(); if !speculative { - // Given something like `U : SomeTrait`, we want to produce a + // Given something like `U: SomeTrait`, we want to produce a // predicate like `::T = X`. This is somewhat // subtle in the event that `T` is defined in a supertrait of // `SomeTrait`, because in that case we need to upcast. @@ -839,7 +839,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { // That is, consider this case: // // ``` - // trait SubTrait : SuperTrait { } + // trait SubTrait: SuperTrait { } // trait SuperTrait { type T; } // // ... B : SubTrait ... @@ -879,6 +879,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } } + let supertraits = traits::supertraits(tcx, trait_ref); let candidate = if self.trait_defines_associated_type_named(trait_ref.def_id(), binding.item_name) { // Simple case: X is defined in the current trait. @@ -886,10 +887,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } else { // Otherwise, we have to walk through the supertraits to find // those that do. - let candidates = traits::supertraits(tcx, trait_ref).filter(|r| { + let candidates = supertraits.filter(|r| { self.trait_defines_associated_type_named(r.def_id(), binding.item_name) }); - self.one_bound_for_assoc_type(candidates, &trait_ref.to_string(), + let candidates = candidates.collect::>(); + debug!("foo: candidates: {:?}", candidates); + self.one_bound_for_assoc_type(candidates.into_iter(), &trait_ref.to_string(), binding.item_name, binding.span) }?; @@ -905,6 +908,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } tcx.check_stability(assoc_ty.def_id, Some(ref_id), binding.span); + debug!("foo: info: {:?} {:?} {:?} {:?} {:?}", trait_ref, binding.item_name, speculative, assoc_ty.def_id, dup_bindings); if !speculative { dup_bindings.entry(assoc_ty.def_id) .and_modify(|prev_span| { @@ -921,6 +925,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { }) .or_insert(binding.span); } + static mut ABC: u32 = 0; + unsafe { + ABC += 1; + if ABC == 3 { + assert!(false); + } + }; Ok(candidate.map_bound(|trait_ref| { ty::ProjectionPredicate { @@ -1017,8 +1028,25 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { })); } - for (projection_bound, _) in &projection_bounds { - associated_types.remove(&projection_bound.projection_def_id()); + let mut seen_projection_bounds = FxHashMap::default(); + for (projection_bound, span) in projection_bounds.iter().rev() { + let bound_def_id = projection_bound.projection_def_id(); + seen_projection_bounds.entry(bound_def_id) + .and_modify(|prev_span| { + let assoc_item = tcx.associated_item(bound_def_id); + let trait_def_id = assoc_item.container.id(); + struct_span_err!(tcx.sess, *span, E0719, + "the value of the associated type `{}` (from the trait `{}`) \ + is already specified", + assoc_item.ident, + tcx.item_path_str(trait_def_id)) + .span_label(*span, "re-bound here") + .span_label(*prev_span, format!("binding for `{}`", assoc_item.ident)) + .emit(); + }) + .or_insert(*span); + + associated_types.remove(&bound_def_id); } for item_def_id in associated_types { @@ -1132,7 +1160,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { span) } - // Checks that bounds contains exactly one element and reports appropriate + // Checks that `bounds` contains exactly one element and reports appropriate // errors otherwise. fn one_bound_for_assoc_type(&self, mut bounds: I, diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 53f536578632c..8574443190d7c 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -28,9 +28,9 @@ use errors::{DiagnosticBuilder, DiagnosticId}; use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap}; use rustc::hir; -/// Helper type of a temporary returned by .for_item(...). +/// Helper type of a temporary returned by `.for_item(...)`. /// Necessary because we can't write the following bound: -/// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(FnCtxt<'b, 'gcx, 'tcx>). +/// `F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(FnCtxt<'b, 'gcx, 'tcx>)`. struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { inherited: super::InheritedBuilder<'a, 'gcx, 'tcx>, id: ast::NodeId, diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index be09cfce8cae6..d5f5cbb562e78 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1978,9 +1978,9 @@ pub enum SizedByDefault { No, } -/// Translate the AST's notion of ty param bounds (which are an enum consisting of a newtyped Ty or -/// a region) to ty's notion of ty param bounds, which can either be user-defined traits, or the -/// built-in trait (formerly known as kind): Send. +/// Translate the AST's notion of ty param bounds (which are an enum consisting of a newtyped `Ty` +/// or a region) to ty's notion of ty param bounds, which can either be user-defined traits, or the +/// built-in trait `Send`. pub fn compute_bounds<'gcx: 'tcx, 'tcx>( astconv: &dyn AstConv<'gcx, 'tcx>, param_ty: Ty<'tcx>, @@ -1988,8 +1988,8 @@ pub fn compute_bounds<'gcx: 'tcx, 'tcx>( sized_by_default: SizedByDefault, span: Span, ) -> Bounds<'tcx> { - let mut region_bounds = vec![]; - let mut trait_bounds = vec![]; + let mut region_bounds = Vec::new(); + let mut trait_bounds = Vec::new(); for ast_bound in ast_bounds { match *ast_bound { @@ -1999,7 +1999,7 @@ pub fn compute_bounds<'gcx: 'tcx, 'tcx>( } } - let mut projection_bounds = vec![]; + let mut projection_bounds = Vec::new(); let mut trait_bounds: Vec<_> = trait_bounds.iter().map(|&bound| { (astconv.instantiate_poly_trait_ref(bound, param_ty, &mut projection_bounds), bound.span) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index c81aea2465b7b..a985c3e9fdf44 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4909,4 +4909,5 @@ register_diagnostics! { E0641, // cannot cast to/from a pointer with an unknown kind E0645, // trait aliases not finished E0698, // type inside generator must be known in this context + E0719, // duplicate values for associated type binding } diff --git a/src/test/run-pass/issues/issue-24010.rs b/src/test/run-pass/issues/issue-24010.rs new file mode 100644 index 0000000000000..cce8bb84837f3 --- /dev/null +++ b/src/test/run-pass/issues/issue-24010.rs @@ -0,0 +1,22 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait Foo: Fn(i32) -> i32 + Send {} + +impl i32 + Send> Foo for T {} + +fn wants_foo(f: Box) -> i32 { + f(42) +} + +fn main() { + let f = Box::new(|x| x); + assert_eq!(wants_foo(f), 42); +} diff --git a/src/test/run-pass/traits/trait-alias-object-type.rs b/src/test/run-pass/traits/trait-alias-objects.rs similarity index 80% rename from src/test/run-pass/traits/trait-alias-object-type.rs rename to src/test/run-pass/traits/trait-alias-objects.rs index 17e30922b2cb8..a5bb0cac251b2 100644 --- a/src/test/run-pass/traits/trait-alias-object-type.rs +++ b/src/test/run-pass/traits/trait-alias-objects.rs @@ -21,7 +21,6 @@ pub fn main() { let b = Box::new(456) as Box; assert!(*b == 456); - // FIXME(alexreg): associated type should be gotten from trait alias definition - // let c: &dyn I32Iterator = &vec![123].into_iter(); - // assert_eq!(c.next(), Some(123)); + let c: &mut dyn I32Iterator = &mut vec![123].into_iter(); + assert_eq!(c.next(), Some(123)); } diff --git a/src/test/ui/error-codes/E0191.rs b/src/test/ui/error-codes/E0191.rs index 489ebb033f84e..c35c7e10f5a38 100644 --- a/src/test/ui/error-codes/E0191.rs +++ b/src/test/ui/error-codes/E0191.rs @@ -14,5 +14,4 @@ trait Trait { type Foo = Trait; //~ ERROR E0191 -fn main() { -} +fn main() {} diff --git a/src/test/ui/error-codes/E0719.rs b/src/test/ui/error-codes/E0719.rs new file mode 100644 index 0000000000000..951041124bb1d --- /dev/null +++ b/src/test/ui/error-codes/E0719.rs @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(trait_alias)] + +trait I32Iterator = Iterator; + +pub fn main() { + let _: &I32Iterator; //~ ERROR E0719 +} diff --git a/src/test/ui/issue-51947.rs b/src/test/ui/issue-51947.rs deleted file mode 100644 index 7b79807e4d7ff..0000000000000 --- a/src/test/ui/issue-51947.rs +++ /dev/null @@ -1,17 +0,0 @@ -// compile-pass - -#![crate_type = "lib"] -#![feature(linkage)] - -// MergeFunctions will merge these via an anonymous internal -// backing function, which must be named if ThinLTO buffers are used - -#[linkage = "weak"] -pub fn fn1(a: u32, b: u32, c: u32) -> u32 { - a + b + c -} - -#[linkage = "weak"] -pub fn fn2(a: u32, b: u32, c: u32) -> u32 { - a + b + c -} diff --git a/src/test/ui/issues/issue-51947.rs b/src/test/ui/issues/issue-51947.rs new file mode 100644 index 0000000000000..3e0c3c002f649 --- /dev/null +++ b/src/test/ui/issues/issue-51947.rs @@ -0,0 +1,27 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-pass + +#![crate_type = "lib"] +#![feature(linkage)] + +// MergeFunctions will merge these via an anonymous internal +// backing function, which must be named if ThinLTO buffers are used + +#[linkage = "weak"] +pub fn fn1(a: u32, b: u32, c: u32) -> u32 { + a + b + c +} + +#[linkage = "weak"] +pub fn fn2(a: u32, b: u32, c: u32) -> u32 { + a + b + c +} diff --git a/src/test/ui/traits/trait-alias-associated-type-rebound.rs b/src/test/ui/traits/trait-alias-associated-type-rebound.rs new file mode 100644 index 0000000000000..32f707e192dec --- /dev/null +++ b/src/test/ui/traits/trait-alias-associated-type-rebound.rs @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(trait_alias)] + +trait I32Iterator = Iterator; +trait I32Iterator2 = I32Iterator; +trait U32Iterator = I32Iterator2; + +pub fn main() {} From 6d3ee4170d525612161fa7f4c34315316dd422c5 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Mon, 5 Nov 2018 02:02:43 +0000 Subject: [PATCH 04/26] Added error for duplicate bindings of associated type. --- src/librustc/traits/mod.rs | 7 +- src/librustc/ty/mod.rs | 46 +++---- src/librustc/ty/sty.rs | 18 +-- src/librustc/ty/subst.rs | 14 +- src/librustc_typeck/astconv.rs | 130 +++++++++--------- ...alias-objects.rs => trait-alias-object.rs} | 2 +- .../E0719-trait-alias-object.rs} | 6 +- .../E0719-trait-alias-object.stderr | 12 ++ src/test/ui/error-codes/E0719-trait-alias.rs | 18 +++ .../ui/error-codes/E0719-trait-alias.stderr | 29 ++++ src/test/ui/error-codes/E0719.rs | 8 +- src/test/ui/error-codes/E0719.stderr | 11 ++ ...alias-objects.rs => trait-alias-object.rs} | 0 ...jects.stderr => trait-alias-object.stderr} | 0 14 files changed, 184 insertions(+), 117 deletions(-) rename src/test/run-pass/traits/{trait-alias-objects.rs => trait-alias-object.rs} (91%) rename src/test/ui/{traits/trait-alias-associated-type-rebound.rs => error-codes/E0719-trait-alias-object.rs} (83%) create mode 100644 src/test/ui/error-codes/E0719-trait-alias-object.stderr create mode 100644 src/test/ui/error-codes/E0719-trait-alias.rs create mode 100644 src/test/ui/error-codes/E0719-trait-alias.stderr create mode 100644 src/test/ui/error-codes/E0719.stderr rename src/test/ui/traits/{trait-alias-objects.rs => trait-alias-object.rs} (100%) rename src/test/ui/traits/{trait-alias-objects.stderr => trait-alias-object.stderr} (100%) diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs index b7512790bfb69..33b689c60a118 100644 --- a/src/librustc/traits/mod.rs +++ b/src/librustc/traits/mod.rs @@ -50,11 +50,8 @@ pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError pub use self::specialize::{OverlapError, specialization_graph, translate_substs}; pub use self::specialize::find_associated_item; pub use self::engine::{TraitEngine, TraitEngineExt}; -pub use self::util::elaborate_predicates; -pub use self::util::supertraits; -pub use self::util::Supertraits; -pub use self::util::supertrait_def_ids; -pub use self::util::SupertraitDefIds; +pub use self::util::{elaborate_predicates, elaborate_trait_ref, elaborate_trait_refs}; +pub use self::util::{supertraits, supertrait_def_ids, Supertraits, SupertraitDefIds}; pub use self::util::transitive_bounds; #[allow(dead_code)] diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 71d59750d6c87..19fe8606a4d07 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -544,14 +544,14 @@ impl<'tcx> TyS<'tcx> { pub fn is_primitive_ty(&self) -> bool { match self.sty { TyKind::Bool | - TyKind::Char | - TyKind::Int(_) | - TyKind::Uint(_) | - TyKind::Float(_) | - TyKind::Infer(InferTy::IntVar(_)) | - TyKind::Infer(InferTy::FloatVar(_)) | - TyKind::Infer(InferTy::FreshIntTy(_)) | - TyKind::Infer(InferTy::FreshFloatTy(_)) => true, + TyKind::Char | + TyKind::Int(_) | + TyKind::Uint(_) | + TyKind::Float(_) | + TyKind::Infer(InferTy::IntVar(_)) | + TyKind::Infer(InferTy::FloatVar(_)) | + TyKind::Infer(InferTy::FreshIntTy(_)) | + TyKind::Infer(InferTy::FreshFloatTy(_)) => true, TyKind::Ref(_, x, _) => x.is_primitive_ty(), _ => false, } @@ -1042,15 +1042,15 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> { #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] pub enum Predicate<'tcx> { - /// Corresponds to `where Foo : Bar`. `Foo` here would be + /// Corresponds to `where Foo: Bar`. `Foo` here would be /// the `Self` type of the trait reference and `A`, `B`, and `C` /// would be the type parameters. Trait(PolyTraitPredicate<'tcx>), - /// where `'a : 'b` + /// where `'a: 'b` RegionOutlives(PolyRegionOutlivesPredicate<'tcx>), - /// where `T : 'a` + /// where `T: 'a` TypeOutlives(PolyTypeOutlivesPredicate<'tcx>), /// where `::Name == X`, approximately. @@ -1063,7 +1063,7 @@ pub enum Predicate<'tcx> { /// trait must be object-safe ObjectSafe(DefId), - /// No direct syntax. May be thought of as `where T : FnFoo<...>` + /// No direct syntax. May be thought of as `where T: FnFoo<...>` /// for some substitutions `...` and `T` being a closure type. /// Satisfied (or refuted) once we know the closure's kind. ClosureKind(DefId, ClosureSubsts<'tcx>, ClosureKind), @@ -1112,11 +1112,11 @@ impl<'a, 'gcx, 'tcx> Predicate<'tcx> { // // Let's start with an easy case. Consider two traits: // - // trait Foo<'a> : Bar<'a,'a> { } + // trait Foo<'a>: Bar<'a,'a> { } // trait Bar<'b,'c> { } // - // Now, if we have a trait reference `for<'x> T : Foo<'x>`, then - // we can deduce that `for<'x> T : Bar<'x,'x>`. Basically, if we + // Now, if we have a trait reference `for<'x> T: Foo<'x>`, then + // we can deduce that `for<'x> T: Bar<'x,'x>`. Basically, if we // knew that `Foo<'x>` (for any 'x) then we also know that // `Bar<'x,'x>` (for any 'x). This more-or-less falls out from // normal substitution. @@ -1129,21 +1129,21 @@ impl<'a, 'gcx, 'tcx> Predicate<'tcx> { // // Another example to be careful of is this: // - // trait Foo1<'a> : for<'b> Bar1<'a,'b> { } + // trait Foo1<'a>: for<'b> Bar1<'a,'b> { } // trait Bar1<'b,'c> { } // - // Here, if we have `for<'x> T : Foo1<'x>`, then what do we know? - // The answer is that we know `for<'x,'b> T : Bar1<'x,'b>`. The + // Here, if we have `for<'x> T: Foo1<'x>`, then what do we know? + // The answer is that we know `for<'x,'b> T: Bar1<'x,'b>`. The // reason is similar to the previous example: any impl of - // `T:Foo1<'x>` must show that `for<'b> T : Bar1<'x, 'b>`. So + // `T:Foo1<'x>` must show that `for<'b> T: Bar1<'x, 'b>`. So // basically we would want to collapse the bound lifetimes from // the input (`trait_ref`) and the supertraits. // // To achieve this in practice is fairly straightforward. Let's // consider the more complicated scenario: // - // - We start out with `for<'x> T : Foo1<'x>`. In this case, `'x` - // has a De Bruijn index of 1. We want to produce `for<'x,'b> T : Bar1<'x,'b>`, + // - We start out with `for<'x> T: Foo1<'x>`. In this case, `'x` + // has a De Bruijn index of 1. We want to produce `for<'x,'b> T: Bar1<'x,'b>`, // where both `'x` and `'b` would have a DB index of 1. // The substitution from the input trait-ref is therefore going to be // `'a => 'x` (where `'x` has a DB index of 1). @@ -1219,7 +1219,7 @@ impl<'tcx> PolyTraitPredicate<'tcx> { } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)] -pub struct OutlivesPredicate(pub A, pub B); // `A : B` +pub struct OutlivesPredicate(pub A, pub B); // `A: B` pub type PolyOutlivesPredicate = ty::Binder>; pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate, ty::Region<'tcx>>; @@ -2476,7 +2476,7 @@ impl<'tcx> TyS<'tcx> { /// /// Note: prefer `ty.walk()` where possible. pub fn maybe_walk(&'tcx self, mut f: F) - where F : FnMut(Ty<'tcx>) -> bool + where F: FnMut(Ty<'tcx>) -> bool { let mut walker = self.walk(); while let Some(ty) = walker.next() { diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 28b58d62175bc..5c8549cba2333 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -627,7 +627,7 @@ impl<'tcx> Binder<&'tcx List>> { /// A complete reference to a trait. These take numerous guises in syntax, /// but perhaps the most recognizable form is in a where clause: /// -/// T : Foo +/// T: Foo /// /// This would be represented by a trait-reference where the def-id is the /// def-id for the trait `Foo` and the substs define `T` as parameter 0, @@ -637,8 +637,8 @@ impl<'tcx> Binder<&'tcx List>> { /// that case the `Self` parameter is absent from the substitutions. /// /// Note that a `TraitRef` introduces a level of region binding, to -/// account for higher-ranked trait bounds like `T : for<'a> Foo<&'a -/// U>` or higher-ranked object types. +/// account for higher-ranked trait bounds like `T: for<'a> Foo<&'a U>` +/// or higher-ranked object types. #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] pub struct TraitRef<'tcx> { pub def_id: DefId, @@ -663,7 +663,7 @@ impl<'tcx> TraitRef<'tcx> { self.substs.type_at(0) } - pub fn input_types<'a>(&'a self) -> impl DoubleEndedIterator> + 'a { + pub fn input_types<'a>(&'a self) -> impl DoubleEndedIterator> + 'a { // Select only the "input types" from a trait-reference. For // now this is all the types that appear in the // trait-reference, but it should eventually exclude @@ -886,16 +886,16 @@ pub struct ProjectionTy<'tcx> { /// The parameters of the associated item. pub substs: &'tcx Substs<'tcx>, - /// The DefId of the TraitItem for the associated type N. + /// The `DefId` of the `TraitItem` for the associated type `N`. /// - /// Note that this is not the DefId of the TraitRef containing this - /// associated type, which is in tcx.associated_item(item_def_id).container. + /// Note that this is not the `DefId` of the `TraitRef` containing this + /// associated type, which is in `tcx.associated_item(item_def_id).container`. pub item_def_id: DefId, } impl<'a, 'tcx> ProjectionTy<'tcx> { - /// Construct a ProjectionTy by searching the trait from trait_ref for the - /// associated item named item_name. + /// Construct a `ProjectionTy` by searching the trait from `trait_ref` for the + /// associated item named `item_name`. pub fn from_ref_and_name( tcx: TyCtxt<'_, '_, '_>, trait_ref: ty::TraitRef<'tcx>, item_name: Ident ) -> ProjectionTy<'tcx> { diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index c1aed36c92ddf..b28e7c9fb199b 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -27,7 +27,7 @@ use std::marker::PhantomData; use std::mem; use std::num::NonZeroUsize; -/// An entity in the Rust typesystem, which can be one of +/// An entity in the Rust type system, which can be one of /// several kinds (only types and lifetimes for now). /// To reduce memory usage, a `Kind` is a interned pointer, /// with the lowest 2 bits being reserved for a tag to @@ -171,7 +171,7 @@ impl<'tcx> Decodable for Kind<'tcx> { pub type Substs<'tcx> = List>; impl<'a, 'gcx, 'tcx> Substs<'tcx> { - /// Creates a Substs that maps each generic parameter to itself. + /// Creates a `Substs` that maps each generic parameter to itself. pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId) -> &'tcx Substs<'tcx> { Substs::for_item(tcx, def_id, |param, _| { @@ -179,9 +179,9 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> { }) } - /// Creates a Substs for generic parameter definitions, + /// Creates a `Substs` for generic parameter definitions, /// by calling closures to obtain each kind. - /// The closures get to observe the Substs as they're + /// The closures get to observe the `Substs` as they're /// being built, which can be used to correctly /// substitute defaults of generic parameters. pub fn for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, @@ -242,7 +242,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> { } #[inline] - pub fn types(&'a self) -> impl DoubleEndedIterator> + 'a { + pub fn types(&'a self) -> impl DoubleEndedIterator> + 'a { self.iter().filter_map(|k| { if let UnpackedKind::Type(ty) = k.unpack() { Some(ty) @@ -253,7 +253,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> { } #[inline] - pub fn regions(&'a self) -> impl DoubleEndedIterator> + 'a { + pub fn regions(&'a self) -> impl DoubleEndedIterator> + 'a { self.iter().filter_map(|k| { if let UnpackedKind::Lifetime(lt) = k.unpack() { Some(lt) @@ -332,7 +332,7 @@ impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Substs<'tcx> {} // `foo`. Or use `foo.subst_spanned(tcx, substs, Some(span))` when // there is more information available (for better errors). -pub trait Subst<'tcx> : Sized { +pub trait Subst<'tcx>: Sized { fn subst<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, substs: &[Kind<'tcx>]) -> Self { self.subst_spanned(tcx, substs, None) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 3aaa5810eab38..f3f4251693864 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -37,7 +37,7 @@ use std::iter; use syntax::ast; use syntax::ptr::P; use syntax::feature_gate::{GateIssue, emit_feature_err}; -use syntax_pos::{Span, MultiSpan}; +use syntax_pos::{DUMMY_SP, Span, MultiSpan}; pub trait AstConv<'gcx, 'tcx> { fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx>; @@ -719,6 +719,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { speculative: bool) -> ty::PolyTraitRef<'tcx> { + let tcx = self.tcx(); + let trait_def_id = self.trait_def_id(trait_ref); debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id); @@ -732,16 +734,74 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { trait_ref.path.segments.last().unwrap()); let poly_trait_ref = ty::Binder::bind(ty::TraitRef::new(trait_def_id, substs)); - let mut dup_bindings = FxHashMap::default(); poly_projections.extend(assoc_bindings.iter().filter_map(|binding| { // specify type to assert that error was already reported in Err case: let predicate: Result<_, ErrorReported> = self.ast_type_binding_to_poly_projection_predicate( - trait_ref.ref_id, poly_trait_ref, binding, speculative, &mut dup_bindings); + trait_ref.ref_id, poly_trait_ref, binding, speculative); // okay to ignore Err because of ErrorReported (see above) Some((predicate.ok()?, binding.span)) })); + // make flat_map: + // for tr in traits::supertraits(tcx, poly_trait_ref) { + // let sup_trait_ref = tr.skip_binder(); + // poly_projections.extend(sup_trait_ref.substs.types().filter_map(|t| { + // if let TyKind::Projection(proj) = t.sty { + // Some((proj, span)) + // } else { + // None + // } + // }); + // } + + // Include all projections from associated type bindings of supertraits. + poly_projections.extend(traits::elaborate_trait_ref(tcx, poly_trait_ref) + .into_iter() + .filter_map(|pred| { + if let ty::Predicate::Projection(proj) = pred { + Some(proj) + } else { + None + } + }) + .map(|proj| (proj, DUMMY_SP)) + ); + + // // Include associated type bindings from supertraits. + // let mut foo = poly_projections.clone(); + // foo.extend(tcx.predicates_of(trait_def_id) + // .predicates.into_iter() + // .filter_map(|(pred, span)| { + // debug!("pred: {:?}", pred); + // if let ty::Predicate::Projection(proj) = pred { + // Some((proj, span)) + // } else { + // None + // } + // })); + + // Check for multiple bindings of associated types. + let mut seen_projection_bounds = FxHashMap::default(); + for (projection_bound, span) in poly_projections.iter().rev() { + let bound_def_id = projection_bound.projection_def_id(); + let assoc_item = tcx.associated_item(bound_def_id); + let trait_def_id = assoc_item.container.id(); + // let trait_ref = tcx.associated_item(proj.projection_type.item_def_id).container; + seen_projection_bounds.entry((assoc_item.def_id, bound_def_id)) + .and_modify(|prev_span| { + struct_span_err!(tcx.sess, *span, E0719, + "the value of the associated type `{}` (from the trait `{}`) \ + is already specified", + assoc_item.ident, + tcx.item_path_str(trait_def_id)) + .span_label(*span, "re-bound here") + .span_label(*prev_span, format!("`{}` bound here first", assoc_item.ident)) + .emit(); + }) + .or_insert(*span); + } + debug!("instantiate_poly_trait_ref({:?}, projections={:?}) -> {:?}", trait_ref, poly_projections, poly_trait_ref); poly_trait_ref @@ -824,8 +884,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { ref_id: ast::NodeId, trait_ref: ty::PolyTraitRef<'tcx>, binding: &ConvertedBinding<'tcx>, - speculative: bool, - dup_bindings: &mut FxHashMap) + speculative: bool) -> Result, ErrorReported> { let tcx = self.tcx(); @@ -879,7 +938,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } } - let supertraits = traits::supertraits(tcx, trait_ref); let candidate = if self.trait_defines_associated_type_named(trait_ref.def_id(), binding.item_name) { // Simple case: X is defined in the current trait. @@ -887,11 +945,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } else { // Otherwise, we have to walk through the supertraits to find // those that do. - let candidates = supertraits.filter(|r| { + let candidates = traits::supertraits(tcx, trait_ref).filter(|r| { self.trait_defines_associated_type_named(r.def_id(), binding.item_name) }); - let candidates = candidates.collect::>(); - debug!("foo: candidates: {:?}", candidates); self.one_bound_for_assoc_type(candidates.into_iter(), &trait_ref.to_string(), binding.item_name, binding.span) }?; @@ -908,31 +964,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } tcx.check_stability(assoc_ty.def_id, Some(ref_id), binding.span); - debug!("foo: info: {:?} {:?} {:?} {:?} {:?}", trait_ref, binding.item_name, speculative, assoc_ty.def_id, dup_bindings); - if !speculative { - dup_bindings.entry(assoc_ty.def_id) - .and_modify(|prev_span| { - let mut err = self.tcx().struct_span_lint_node( - ::rustc::lint::builtin::DUPLICATE_ASSOCIATED_TYPE_BINDINGS, - ref_id, - binding.span, - &format!("associated type binding `{}` specified more than once", - binding.item_name) - ); - err.span_label(binding.span, "used more than once"); - err.span_label(*prev_span, format!("first use of `{}`", binding.item_name)); - err.emit(); - }) - .or_insert(binding.span); - } - static mut ABC: u32 = 0; - unsafe { - ABC += 1; - if ABC == 3 { - assert!(false); - } - }; - Ok(candidate.map_bound(|trait_ref| { ty::ProjectionPredicate { projection_ty: ty::ProjectionTy::from_ref_and_name( @@ -1016,37 +1047,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { associated_types.extend(tcx.associated_items(tr.def_id()) .filter(|item| item.kind == ty::AssociatedKind::Type) .map(|item| item.def_id)); - - projection_bounds.extend(tcx.predicates_of(tr.def_id()) - .predicates.into_iter() - .filter_map(|(pred, span)| { - if let ty::Predicate::Projection(proj) = pred { - Some((proj, span)) - } else { - None - } - })); } - let mut seen_projection_bounds = FxHashMap::default(); - for (projection_bound, span) in projection_bounds.iter().rev() { - let bound_def_id = projection_bound.projection_def_id(); - seen_projection_bounds.entry(bound_def_id) - .and_modify(|prev_span| { - let assoc_item = tcx.associated_item(bound_def_id); - let trait_def_id = assoc_item.container.id(); - struct_span_err!(tcx.sess, *span, E0719, - "the value of the associated type `{}` (from the trait `{}`) \ - is already specified", - assoc_item.ident, - tcx.item_path_str(trait_def_id)) - .span_label(*span, "re-bound here") - .span_label(*prev_span, format!("binding for `{}`", assoc_item.ident)) - .emit(); - }) - .or_insert(*span); - - associated_types.remove(&bound_def_id); + for (projection_bound, _) in projection_bounds.iter().rev() { + associated_types.remove(&projection_bound.projection_def_id()); } for item_def_id in associated_types { diff --git a/src/test/run-pass/traits/trait-alias-objects.rs b/src/test/run-pass/traits/trait-alias-object.rs similarity index 91% rename from src/test/run-pass/traits/trait-alias-objects.rs rename to src/test/run-pass/traits/trait-alias-object.rs index a5bb0cac251b2..adac28eeb1292 100644 --- a/src/test/run-pass/traits/trait-alias-objects.rs +++ b/src/test/run-pass/traits/trait-alias-object.rs @@ -21,6 +21,6 @@ pub fn main() { let b = Box::new(456) as Box; assert!(*b == 456); - let c: &mut dyn I32Iterator = &mut vec![123].into_iter(); + let c: &mut dyn I32Iterator = &mut vec![123].into_iter(); assert_eq!(c.next(), Some(123)); } diff --git a/src/test/ui/traits/trait-alias-associated-type-rebound.rs b/src/test/ui/error-codes/E0719-trait-alias-object.rs similarity index 83% rename from src/test/ui/traits/trait-alias-associated-type-rebound.rs rename to src/test/ui/error-codes/E0719-trait-alias-object.rs index 32f707e192dec..1e842e5b508e9 100644 --- a/src/test/ui/traits/trait-alias-associated-type-rebound.rs +++ b/src/test/ui/error-codes/E0719-trait-alias-object.rs @@ -11,7 +11,7 @@ #![feature(trait_alias)] trait I32Iterator = Iterator; -trait I32Iterator2 = I32Iterator; -trait U32Iterator = I32Iterator2; -pub fn main() {} +fn main() { + let _: &I32Iterator; //~ ERROR E0719 +} diff --git a/src/test/ui/error-codes/E0719-trait-alias-object.stderr b/src/test/ui/error-codes/E0719-trait-alias-object.stderr new file mode 100644 index 0000000000000..17ebf5901c438 --- /dev/null +++ b/src/test/ui/error-codes/E0719-trait-alias-object.stderr @@ -0,0 +1,12 @@ +error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified + --> $DIR/E0719-trait-alias-object.rs:16:25 + | +LL | trait I32Iterator = Iterator; + | ---------- `Item` bound here first +... +LL | let _: &I32Iterator; //~ ERROR E0719 + | ^^^^^^^^^^ re-bound here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0719`. diff --git a/src/test/ui/error-codes/E0719-trait-alias.rs b/src/test/ui/error-codes/E0719-trait-alias.rs new file mode 100644 index 0000000000000..4232cafa58b07 --- /dev/null +++ b/src/test/ui/error-codes/E0719-trait-alias.rs @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(trait_alias)] + +trait I32Iterator = Iterator; +trait I32Iterator2 = I32Iterator; //~ ERROR E0719 +trait U32Iterator = I32Iterator2; //~ ERROR E0719 +trait U32Iterator2 = U32Iterator; //~ ERROR E0719 + +fn main() {} diff --git a/src/test/ui/error-codes/E0719-trait-alias.stderr b/src/test/ui/error-codes/E0719-trait-alias.stderr new file mode 100644 index 0000000000000..6bb1a541f4d44 --- /dev/null +++ b/src/test/ui/error-codes/E0719-trait-alias.stderr @@ -0,0 +1,29 @@ +error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified + --> $DIR/E0719-trait-alias.rs:14:34 + | +LL | trait I32Iterator = Iterator; + | ---------- `Item` bound here first +LL | trait I32Iterator2 = I32Iterator; //~ ERROR E0719 + | ^^^^^^^^^^ re-bound here + +error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified + --> $DIR/E0719-trait-alias.rs:15:34 + | +LL | trait I32Iterator = Iterator; + | ---------- `Item` bound here first +LL | trait I32Iterator2 = I32Iterator; //~ ERROR E0719 +LL | trait U32Iterator = I32Iterator2; //~ ERROR E0719 + | ^^^^^^^^^^ re-bound here + +error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified + --> $DIR/E0719-trait-alias.rs:16:34 + | +LL | trait I32Iterator = Iterator; + | ---------- `Item` bound here first +... +LL | trait U32Iterator2 = U32Iterator; //~ ERROR E0719 + | ^^^^^^^^^^ re-bound here + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0719`. diff --git a/src/test/ui/error-codes/E0719.rs b/src/test/ui/error-codes/E0719.rs index 951041124bb1d..2177d29110abb 100644 --- a/src/test/ui/error-codes/E0719.rs +++ b/src/test/ui/error-codes/E0719.rs @@ -8,10 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(trait_alias)] - -trait I32Iterator = Iterator; - -pub fn main() { - let _: &I32Iterator; //~ ERROR E0719 +fn main() { + let _: &Iterator; //~ ERROR E0719 } diff --git a/src/test/ui/error-codes/E0719.stderr b/src/test/ui/error-codes/E0719.stderr new file mode 100644 index 0000000000000..78a71b6faabd3 --- /dev/null +++ b/src/test/ui/error-codes/E0719.stderr @@ -0,0 +1,11 @@ +error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified + --> $DIR/E0719.rs:12:22 + | +LL | let _: &Iterator; //~ ERROR E0719 + | ^^^^^^^^^^ ---------- `Item` bound here first + | | + | re-bound here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0719`. diff --git a/src/test/ui/traits/trait-alias-objects.rs b/src/test/ui/traits/trait-alias-object.rs similarity index 100% rename from src/test/ui/traits/trait-alias-objects.rs rename to src/test/ui/traits/trait-alias-object.rs diff --git a/src/test/ui/traits/trait-alias-objects.stderr b/src/test/ui/traits/trait-alias-object.stderr similarity index 100% rename from src/test/ui/traits/trait-alias-objects.stderr rename to src/test/ui/traits/trait-alias-object.stderr From 7d9ee0314ac6755700ae8f51c9e00b2e5066250a Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Tue, 6 Nov 2018 23:17:11 +0000 Subject: [PATCH 05/26] Only do check for trait objects, not trait or trait alias definitions. --- src/librustc/ty/mod.rs | 5 +- src/librustc_typeck/astconv.rs | 117 +++++++----------- .../associated-types-from-supertrait.rs} | 13 +- .../associated-types-overridden-binding-2.rs} | 2 +- ...sociated-types-overridden-binding-2.stderr | 13 ++ .../associated-types-overridden-binding.rs} | 11 +- ...associated-types-overridden-binding.stderr | 15 +++ .../E0719-trait-alias-object.stderr | 12 -- .../ui/error-codes/E0719-trait-alias.stderr | 29 ----- src/test/ui/error-codes/E0719.rs | 11 +- src/test/ui/error-codes/E0719.stderr | 20 ++- ...sue-50589-multiple-associated-types.stderr | 23 ---- src/test/ui/traits/trait-alias-object.stderr | 4 +- 13 files changed, 112 insertions(+), 163 deletions(-) rename src/test/{ui/lint/issue-50589-multiple-associated-types.rs => run-pass/associated-types/associated-types-from-supertrait.rs} (75%) rename src/test/ui/{error-codes/E0719-trait-alias-object.rs => associated-types/associated-types-overridden-binding-2.rs} (90%) create mode 100644 src/test/ui/associated-types/associated-types-overridden-binding-2.stderr rename src/test/ui/{error-codes/E0719-trait-alias.rs => associated-types/associated-types-overridden-binding.rs} (72%) create mode 100644 src/test/ui/associated-types/associated-types-overridden-binding.stderr delete mode 100644 src/test/ui/error-codes/E0719-trait-alias-object.stderr delete mode 100644 src/test/ui/error-codes/E0719-trait-alias.stderr delete mode 100644 src/test/ui/lint/issue-50589-multiple-associated-types.stderr diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 19fe8606a4d07..ef9b3e3efab27 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1195,6 +1195,7 @@ impl<'a, 'gcx, 'tcx> Predicate<'tcx> { pub struct TraitPredicate<'tcx> { pub trait_ref: TraitRef<'tcx> } + pub type PolyTraitPredicate<'tcx> = ty::Binder>; impl<'tcx> TraitPredicate<'tcx> { @@ -1516,14 +1517,14 @@ impl UniverseIndex { UniverseIndex::from_u32(self.private.checked_add(1).unwrap()) } - /// `true` if `self` can name a name from `other` -- in other words, + /// Returns `true` if `self` can name a name from `other` -- in other words, /// if the set of names in `self` is a superset of those in /// `other` (`self >= other`). pub fn can_name(self, other: UniverseIndex) -> bool { self.private >= other.private } - /// `true` if `self` cannot name some names from `other` -- in other + /// Returns `true` if `self` cannot name some names from `other` -- in other /// words, if the set of names in `self` is a strict subset of /// those in `other` (`self < other`). pub fn cannot_name(self, other: UniverseIndex) -> bool { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index f3f4251693864..572e79407a10b 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -719,8 +719,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { speculative: bool) -> ty::PolyTraitRef<'tcx> { - let tcx = self.tcx(); - let trait_def_id = self.trait_def_id(trait_ref); debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id); @@ -734,74 +732,16 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { trait_ref.path.segments.last().unwrap()); let poly_trait_ref = ty::Binder::bind(ty::TraitRef::new(trait_def_id, substs)); + let mut dup_bindings = FxHashMap::default(); poly_projections.extend(assoc_bindings.iter().filter_map(|binding| { // specify type to assert that error was already reported in Err case: let predicate: Result<_, ErrorReported> = self.ast_type_binding_to_poly_projection_predicate( - trait_ref.ref_id, poly_trait_ref, binding, speculative); + trait_ref.ref_id, poly_trait_ref, binding, speculative, &mut dup_bindings); // okay to ignore Err because of ErrorReported (see above) Some((predicate.ok()?, binding.span)) })); - // make flat_map: - // for tr in traits::supertraits(tcx, poly_trait_ref) { - // let sup_trait_ref = tr.skip_binder(); - // poly_projections.extend(sup_trait_ref.substs.types().filter_map(|t| { - // if let TyKind::Projection(proj) = t.sty { - // Some((proj, span)) - // } else { - // None - // } - // }); - // } - - // Include all projections from associated type bindings of supertraits. - poly_projections.extend(traits::elaborate_trait_ref(tcx, poly_trait_ref) - .into_iter() - .filter_map(|pred| { - if let ty::Predicate::Projection(proj) = pred { - Some(proj) - } else { - None - } - }) - .map(|proj| (proj, DUMMY_SP)) - ); - - // // Include associated type bindings from supertraits. - // let mut foo = poly_projections.clone(); - // foo.extend(tcx.predicates_of(trait_def_id) - // .predicates.into_iter() - // .filter_map(|(pred, span)| { - // debug!("pred: {:?}", pred); - // if let ty::Predicate::Projection(proj) = pred { - // Some((proj, span)) - // } else { - // None - // } - // })); - - // Check for multiple bindings of associated types. - let mut seen_projection_bounds = FxHashMap::default(); - for (projection_bound, span) in poly_projections.iter().rev() { - let bound_def_id = projection_bound.projection_def_id(); - let assoc_item = tcx.associated_item(bound_def_id); - let trait_def_id = assoc_item.container.id(); - // let trait_ref = tcx.associated_item(proj.projection_type.item_def_id).container; - seen_projection_bounds.entry((assoc_item.def_id, bound_def_id)) - .and_modify(|prev_span| { - struct_span_err!(tcx.sess, *span, E0719, - "the value of the associated type `{}` (from the trait `{}`) \ - is already specified", - assoc_item.ident, - tcx.item_path_str(trait_def_id)) - .span_label(*span, "re-bound here") - .span_label(*prev_span, format!("`{}` bound here first", assoc_item.ident)) - .emit(); - }) - .or_insert(*span); - } - debug!("instantiate_poly_trait_ref({:?}, projections={:?}) -> {:?}", trait_ref, poly_projections, poly_trait_ref); poly_trait_ref @@ -884,7 +824,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { ref_id: ast::NodeId, trait_ref: ty::PolyTraitRef<'tcx>, binding: &ConvertedBinding<'tcx>, - speculative: bool) + speculative: bool, + dup_bindings: &mut FxHashMap) -> Result, ErrorReported> { let tcx = self.tcx(); @@ -948,7 +889,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { let candidates = traits::supertraits(tcx, trait_ref).filter(|r| { self.trait_defines_associated_type_named(r.def_id(), binding.item_name) }); - self.one_bound_for_assoc_type(candidates.into_iter(), &trait_ref.to_string(), + self.one_bound_for_assoc_type(candidates, &trait_ref.to_string(), binding.item_name, binding.span) }?; @@ -964,6 +905,21 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { } tcx.check_stability(assoc_ty.def_id, Some(ref_id), binding.span); + if !speculative { + dup_bindings.entry(assoc_ty.def_id) + .and_modify(|prev_span| { + struct_span_err!(self.tcx().sess, binding.span, E0719, + "the value of the associated type `{}` (from the trait `{}`) \ + is already specified", + binding.item_name, + tcx.item_path_str(assoc_ty.container.id())) + .span_label(binding.span, "re-bound here") + .span_label(*prev_span, format!("`{}` bound here first", binding.item_name)) + .emit(); + }) + .or_insert(binding.span); + } + Ok(candidate.map_bound(|trait_ref| { ty::ProjectionPredicate { projection_ty: ty::ProjectionTy::from_ref_and_name( @@ -989,6 +945,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { ) } + /// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by + /// removing the dummy `Self` type (`TRAIT_OBJECT_DUMMY_SELF`). + fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>) + -> ty::ExistentialTraitRef<'tcx> { + assert_eq!(trait_ref.self_ty().sty, TRAIT_OBJECT_DUMMY_SELF); + ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) + } + fn conv_object_ty_poly_trait_ref(&self, span: Span, trait_bounds: &[hir::PolyTraitRef], @@ -1043,13 +1007,22 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { // Use a `BTreeSet` to keep output in a more consistent order. let mut associated_types = BTreeSet::default(); - for tr in traits::supertraits(tcx, principal) { - associated_types.extend(tcx.associated_items(tr.def_id()) - .filter(|item| item.kind == ty::AssociatedKind::Type) - .map(|item| item.def_id)); + for tr in traits::elaborate_trait_ref(tcx, principal) { + match tr { + ty::Predicate::Trait(pred) => { + associated_types.extend(tcx.associated_items(pred.def_id()) + .filter(|item| item.kind == ty::AssociatedKind::Type) + .map(|item| item.def_id)); + } + ty::Predicate::Projection(pred) => { + // Include projections defined on supertraits. + projection_bounds.push((pred, DUMMY_SP)) + } + _ => () + } } - for (projection_bound, _) in projection_bounds.iter().rev() { + for (projection_bound, _) in &projection_bounds { associated_types.remove(&projection_bound.projection_def_id()); } @@ -1067,13 +1040,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o { // Erase the `dummy_self` (`TRAIT_OBJECT_DUMMY_SELF`) used above. let existential_principal = principal.map_bound(|trait_ref| { - assert_eq!(trait_ref.self_ty().sty, TRAIT_OBJECT_DUMMY_SELF); - ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) + self.trait_ref_to_existential(trait_ref) }); let existential_projections = projection_bounds.iter().map(|(bound, _)| { bound.map_bound(|b| { - let trait_ref = ty::ExistentialTraitRef::erase_self_ty(self.tcx(), - b.projection_ty.trait_ref(tcx)); + let trait_ref = self.trait_ref_to_existential(b.projection_ty.trait_ref(tcx)); ty::ExistentialProjection { ty: b.ty, item_def_id: b.projection_ty.item_def_id, diff --git a/src/test/ui/lint/issue-50589-multiple-associated-types.rs b/src/test/run-pass/associated-types/associated-types-from-supertrait.rs similarity index 75% rename from src/test/ui/lint/issue-50589-multiple-associated-types.rs rename to src/test/run-pass/associated-types/associated-types-from-supertrait.rs index 2c789a139cd3a..e69c0af2be768 100644 --- a/src/test/ui/lint/issue-50589-multiple-associated-types.rs +++ b/src/test/run-pass/associated-types/associated-types-from-supertrait.rs @@ -8,16 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass - -use std::iter::Iterator; - -type Unit = (); - -fn test() -> Box> { - Box::new(None.into_iter()) -} +trait Foo: Iterator {} +trait Bar: Foo {} fn main() { - test(); + let _: &dyn Bar; } diff --git a/src/test/ui/error-codes/E0719-trait-alias-object.rs b/src/test/ui/associated-types/associated-types-overridden-binding-2.rs similarity index 90% rename from src/test/ui/error-codes/E0719-trait-alias-object.rs rename to src/test/ui/associated-types/associated-types-overridden-binding-2.rs index 1e842e5b508e9..8d91561b7d64f 100644 --- a/src/test/ui/error-codes/E0719-trait-alias-object.rs +++ b/src/test/ui/associated-types/associated-types-overridden-binding-2.rs @@ -13,5 +13,5 @@ trait I32Iterator = Iterator; fn main() { - let _: &I32Iterator; //~ ERROR E0719 + let _: &I32Iterator = &vec![42].into_iter(); } diff --git a/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr b/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr new file mode 100644 index 0000000000000..536cd945083a6 --- /dev/null +++ b/src/test/ui/associated-types/associated-types-overridden-binding-2.stderr @@ -0,0 +1,13 @@ +error[E0271]: type mismatch resolving ` as std::iter::Iterator>::Item == i32` + --> $DIR/associated-types-overridden-binding-2.rs:16:39 + | +LL | let _: &I32Iterator = &vec![42].into_iter(); + | ^^^^^^^^^^^^^^^^^^^^^ expected u32, found i32 + | + = note: expected type `u32` + found type `i32` + = note: required for the cast to the object type `dyn I32Iterator` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/error-codes/E0719-trait-alias.rs b/src/test/ui/associated-types/associated-types-overridden-binding.rs similarity index 72% rename from src/test/ui/error-codes/E0719-trait-alias.rs rename to src/test/ui/associated-types/associated-types-overridden-binding.rs index 4232cafa58b07..ed2211ecffd2b 100644 --- a/src/test/ui/error-codes/E0719-trait-alias.rs +++ b/src/test/ui/associated-types/associated-types-overridden-binding.rs @@ -10,9 +10,12 @@ #![feature(trait_alias)] +trait Foo: Iterator {} +trait Bar: Foo {} + trait I32Iterator = Iterator; -trait I32Iterator2 = I32Iterator; //~ ERROR E0719 -trait U32Iterator = I32Iterator2; //~ ERROR E0719 -trait U32Iterator2 = U32Iterator; //~ ERROR E0719 +trait U32Iterator = I32Iterator; -fn main() {} +fn main() { + let _: &I32Iterator; +} diff --git a/src/test/ui/associated-types/associated-types-overridden-binding.stderr b/src/test/ui/associated-types/associated-types-overridden-binding.stderr new file mode 100644 index 0000000000000..216aa097db9fb --- /dev/null +++ b/src/test/ui/associated-types/associated-types-overridden-binding.stderr @@ -0,0 +1,15 @@ +error[E0284]: type annotations required: cannot resolve `::Item == i32` + --> $DIR/associated-types-overridden-binding.rs:14:1 + | +LL | trait Bar: Foo {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: required by `Foo` + --> $DIR/associated-types-overridden-binding.rs:13:1 + | +LL | trait Foo: Iterator {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0284`. diff --git a/src/test/ui/error-codes/E0719-trait-alias-object.stderr b/src/test/ui/error-codes/E0719-trait-alias-object.stderr deleted file mode 100644 index 17ebf5901c438..0000000000000 --- a/src/test/ui/error-codes/E0719-trait-alias-object.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified - --> $DIR/E0719-trait-alias-object.rs:16:25 - | -LL | trait I32Iterator = Iterator; - | ---------- `Item` bound here first -... -LL | let _: &I32Iterator; //~ ERROR E0719 - | ^^^^^^^^^^ re-bound here - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0719`. diff --git a/src/test/ui/error-codes/E0719-trait-alias.stderr b/src/test/ui/error-codes/E0719-trait-alias.stderr deleted file mode 100644 index 6bb1a541f4d44..0000000000000 --- a/src/test/ui/error-codes/E0719-trait-alias.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified - --> $DIR/E0719-trait-alias.rs:14:34 - | -LL | trait I32Iterator = Iterator; - | ---------- `Item` bound here first -LL | trait I32Iterator2 = I32Iterator; //~ ERROR E0719 - | ^^^^^^^^^^ re-bound here - -error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified - --> $DIR/E0719-trait-alias.rs:15:34 - | -LL | trait I32Iterator = Iterator; - | ---------- `Item` bound here first -LL | trait I32Iterator2 = I32Iterator; //~ ERROR E0719 -LL | trait U32Iterator = I32Iterator2; //~ ERROR E0719 - | ^^^^^^^^^^ re-bound here - -error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified - --> $DIR/E0719-trait-alias.rs:16:34 - | -LL | trait I32Iterator = Iterator; - | ---------- `Item` bound here first -... -LL | trait U32Iterator2 = U32Iterator; //~ ERROR E0719 - | ^^^^^^^^^^ re-bound here - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0719`. diff --git a/src/test/ui/error-codes/E0719.rs b/src/test/ui/error-codes/E0719.rs index 2177d29110abb..c7bfa85093f2d 100644 --- a/src/test/ui/error-codes/E0719.rs +++ b/src/test/ui/error-codes/E0719.rs @@ -8,6 +8,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +trait Foo: Iterator {} + +type Unit = (); + +fn test() -> Box> { + Box::new(None.into_iter()) +} + fn main() { - let _: &Iterator; //~ ERROR E0719 + let _: &Iterator; + test(); } diff --git a/src/test/ui/error-codes/E0719.stderr b/src/test/ui/error-codes/E0719.stderr index 78a71b6faabd3..3a908fceced60 100644 --- a/src/test/ui/error-codes/E0719.stderr +++ b/src/test/ui/error-codes/E0719.stderr @@ -1,11 +1,19 @@ error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified - --> $DIR/E0719.rs:12:22 + --> $DIR/E0719.rs:11:33 | -LL | let _: &Iterator; //~ ERROR E0719 - | ^^^^^^^^^^ ---------- `Item` bound here first - | | - | re-bound here +LL | trait Foo: Iterator {} + | ---------- ^^^^^^^^^^ re-bound here + | | + | `Item` bound here first -error: aborting due to previous error +error[E0719]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) is already specified + --> $DIR/E0719.rs:15:38 + | +LL | fn test() -> Box> { + | --------- ^^^^^^^^^^^ re-bound here + | | + | `Item` bound here first + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0719`. diff --git a/src/test/ui/lint/issue-50589-multiple-associated-types.stderr b/src/test/ui/lint/issue-50589-multiple-associated-types.stderr deleted file mode 100644 index 7f0a1ee1f3307..0000000000000 --- a/src/test/ui/lint/issue-50589-multiple-associated-types.stderr +++ /dev/null @@ -1,23 +0,0 @@ -warning: associated type binding `Item` specified more than once - --> $DIR/issue-50589-multiple-associated-types.rs:17:39 - | -LL | fn test() -> Box> { - | --------- ^^^^^^^^^^^ used more than once - | | - | first use of `Item` - | - = note: #[warn(duplicate_associated_type_bindings)] on by default - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #50589 - -warning: associated type binding `Item` specified more than once - --> $DIR/issue-50589-multiple-associated-types.rs:17:39 - | -LL | fn test() -> Box> { - | --------- ^^^^^^^^^^^ used more than once - | | - | first use of `Item` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #50589 - diff --git a/src/test/ui/traits/trait-alias-object.stderr b/src/test/ui/traits/trait-alias-object.stderr index 8f9681e898fe8..6b7b322a53d9e 100644 --- a/src/test/ui/traits/trait-alias-object.stderr +++ b/src/test/ui/traits/trait-alias-object.stderr @@ -1,5 +1,5 @@ error[E0038]: the trait `EqAlias` cannot be made into an object - --> $DIR/trait-alias-objects.rs:17:13 + --> $DIR/trait-alias-object.rs:17:13 | LL | let _: &dyn EqAlias = &123; | ^^^^^^^^^^^ the trait `EqAlias` cannot be made into an object @@ -7,7 +7,7 @@ LL | let _: &dyn EqAlias = &123; = note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses error[E0191]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) must be specified - --> $DIR/trait-alias-objects.rs:18:13 + --> $DIR/trait-alias-object.rs:18:13 | LL | let _: &dyn IteratorAlias = &vec![123].into_iter(); | ^^^^^^^^^^^^^^^^^ missing associated type `Item` value From 90a14389d1823e4b8dc4ee2e306dd953e1992e08 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Wed, 7 Nov 2018 21:47:19 +0000 Subject: [PATCH 06/26] Removed `DUPLICATE_ASSOCIATED_TYPE_BINDINGS` lint. This has been replaced by the hard error E0719. --- src/librustc/lint/builtin.rs | 7 ------- src/librustc_lint/lib.rs | 5 ----- 2 files changed, 12 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 266b1c4d4a084..01d87bdbf6337 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -300,12 +300,6 @@ declare_lint! { "detects labels that are never used" } -declare_lint! { - pub DUPLICATE_ASSOCIATED_TYPE_BINDINGS, - Warn, - "warns about duplicate associated type bindings in generics" -} - declare_lint! { pub DUPLICATE_MACRO_EXPORTS, Deny, @@ -418,7 +412,6 @@ impl LintPass for HardwiredLints { ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, UNSTABLE_NAME_COLLISIONS, IRREFUTABLE_LET_PATTERNS, - DUPLICATE_ASSOCIATED_TYPE_BINDINGS, DUPLICATE_MACRO_EXPORTS, INTRA_DOC_LINK_RESOLUTION_FAILURE, MISSING_DOC_CODE_EXAMPLES, diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index f289acc032be8..8c0e9bd11a173 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -317,11 +317,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { reference: "issue #51443 ", edition: None, }, - FutureIncompatibleInfo { - id: LintId::of(DUPLICATE_ASSOCIATED_TYPE_BINDINGS), - reference: "issue #50589 ", - edition: None, - }, FutureIncompatibleInfo { id: LintId::of(PROC_MACRO_DERIVE_RESOLUTION_FALLBACK), reference: "issue #50504 ", From 5b2314b3cacf10e79082180c24c79599e63dbaa9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 1 Nov 2018 19:13:11 +1100 Subject: [PATCH 07/26] Use `SmallVec` outparams in several functions. This avoids some allocations, reducing instruction counts by 1% on a couple of benchmarks. --- src/librustc/infer/opaque_types/mod.rs | 3 ++- src/librustc/infer/outlives/obligations.rs | 17 ++++++------ src/librustc/infer/outlives/verify.rs | 3 ++- src/librustc/traits/util.rs | 4 ++- src/librustc/ty/outlives.rs | 27 ++++++++++--------- src/librustc/ty/sty.rs | 26 +++++++++--------- .../transform/cleanup_post_borrowck.rs | 7 ++++- .../implied_outlives_bounds.rs | 6 +++-- src/librustc_typeck/outlives/utils.rs | 5 +++- 9 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/librustc/infer/opaque_types/mod.rs b/src/librustc/infer/opaque_types/mod.rs index 49858972416d8..cc73dd63816aa 100644 --- a/src/librustc/infer/opaque_types/mod.rs +++ b/src/librustc/infer/opaque_types/mod.rs @@ -366,7 +366,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { let mut types = vec![concrete_ty]; let bound_region = |r| self.sub_regions(infer::CallReturn(span), least_region, r); while let Some(ty) = types.pop() { - let mut components = self.tcx.outlives_components(ty); + let mut components = smallvec![]; + self.tcx.push_outlives_components(ty, &mut components); while let Some(component) = components.pop() { match component { Component::Region(r) => { diff --git a/src/librustc/infer/outlives/obligations.rs b/src/librustc/infer/outlives/obligations.rs index 523f03c2cfc47..f2825887f36e2 100644 --- a/src/librustc/infer/outlives/obligations.rs +++ b/src/librustc/infer/outlives/obligations.rs @@ -9,7 +9,7 @@ // except according to those terms. //! Code that handles "type-outlives" constraints like `T: 'a`. This -//! is based on the `outlives_components` function defined on the tcx, +//! is based on the `push_outlives_components` function defined on the tcx, //! but it adds a bit of heuristics on top, in particular to deal with //! associated types and projections. //! @@ -307,17 +307,18 @@ where assert!(!ty.has_escaping_bound_vars()); - let components = self.tcx.outlives_components(ty); - self.components_must_outlive(origin, components, region); + let mut components = smallvec![]; + self.tcx.push_outlives_components(ty, &mut components); + self.components_must_outlive(origin, &components, region); } fn components_must_outlive( &mut self, origin: infer::SubregionOrigin<'tcx>, - components: Vec>, + components: &[Component<'tcx>], region: ty::Region<'tcx>, ) { - for component in components { + for component in components.iter() { let origin = origin.clone(); match component { Component::Region(region1) => { @@ -325,13 +326,13 @@ where .push_sub_region_constraint(origin, region, region1); } Component::Param(param_ty) => { - self.param_ty_must_outlive(origin, region, param_ty); + self.param_ty_must_outlive(origin, region, *param_ty); } Component::Projection(projection_ty) => { - self.projection_must_outlive(origin, region, projection_ty); + self.projection_must_outlive(origin, region, *projection_ty); } Component::EscapingProjection(subcomponents) => { - self.components_must_outlive(origin, subcomponents, region); + self.components_must_outlive(origin, &subcomponents, region); } Component::UnresolvedInferenceVariable(v) => { // ignore this, we presume it will yield an error diff --git a/src/librustc/infer/outlives/verify.rs b/src/librustc/infer/outlives/verify.rs index 88d45671b9afd..a7a79dd2e6560 100644 --- a/src/librustc/infer/outlives/verify.rs +++ b/src/librustc/infer/outlives/verify.rs @@ -155,7 +155,8 @@ impl<'cx, 'gcx, 'tcx> VerifyBoundCx<'cx, 'gcx, 'tcx> { .map(|subty| self.type_bound(subty)) .collect::>(); - let mut regions = ty.regions(); + let mut regions = smallvec![]; + ty.push_regions(&mut regions); regions.retain(|r| !r.is_late_bound()); // ignore late-bound regions bounds.push(VerifyBound::AllBounds( regions diff --git a/src/librustc/traits/util.rs b/src/librustc/traits/util.rs index 74f8d67ce0484..9a2f484125de7 100644 --- a/src/librustc/traits/util.rs +++ b/src/librustc/traits/util.rs @@ -200,8 +200,10 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> { } let visited = &mut self.visited; + let mut components = smallvec![]; + tcx.push_outlives_components(ty_max, &mut components); self.stack.extend( - tcx.outlives_components(ty_max) + components .into_iter() .filter_map(|component| match component { Component::Region(r) => if r.is_late_bound() { diff --git a/src/librustc/ty/outlives.rs b/src/librustc/ty/outlives.rs index 449730c9d0601..7fac88a3d78f1 100644 --- a/src/librustc/ty/outlives.rs +++ b/src/librustc/ty/outlives.rs @@ -12,6 +12,7 @@ // refers to rules defined in RFC 1214 (`OutlivesFooBar`), so see that // RFC for reference. +use smallvec::SmallVec; use ty::{self, Ty, TyCtxt, TypeFoldable}; #[derive(Debug)] @@ -55,17 +56,15 @@ pub enum Component<'tcx> { } impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { - /// Returns all the things that must outlive `'a` for the condition + /// Push onto `out` all the things that must outlive `'a` for the condition /// `ty0: 'a` to hold. Note that `ty0` must be a **fully resolved type**. - pub fn outlives_components(&self, ty0: Ty<'tcx>) - -> Vec> { - let mut components = vec![]; - self.compute_components(ty0, &mut components); - debug!("components({:?}) = {:?}", ty0, components); - components + pub fn push_outlives_components(&self, ty0: Ty<'tcx>, + out: &mut SmallVec<[Component<'tcx>; 4]>) { + self.compute_components(ty0, out); + debug!("components({:?}) = {:?}", ty0, out); } - fn compute_components(&self, ty: Ty<'tcx>, out: &mut Vec>) { + fn compute_components(&self, ty: Ty<'tcx>, out: &mut SmallVec<[Component<'tcx>; 4]>) { // Descend through the types, looking for the various "base" // components and collecting them into `out`. This is not written // with `collect()` because of the need to sometimes skip subtrees @@ -164,7 +163,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { // list is maintained explicitly, because bound regions // themselves can be readily identified. - push_region_constraints(out, ty.regions()); + push_region_constraints(ty, out); for subty in ty.walk_shallow() { self.compute_components(subty, out); } @@ -173,15 +172,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } fn capture_components(&self, ty: Ty<'tcx>) -> Vec> { - let mut temp = vec![]; - push_region_constraints(&mut temp, ty.regions()); + let mut temp = smallvec![]; + push_region_constraints(ty, &mut temp); for subty in ty.walk_shallow() { self.compute_components(subty, &mut temp); } - temp + temp.into_iter().collect() } } -fn push_region_constraints<'tcx>(out: &mut Vec>, regions: Vec>) { +fn push_region_constraints<'tcx>(ty: Ty<'tcx>, out: &mut SmallVec<[Component<'tcx>; 4]>) { + let mut regions = smallvec![]; + ty.push_regions(&mut regions); out.extend(regions.iter().filter(|&r| !r.is_late_bound()).map(|r| Component::Region(r))); } diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 28b58d62175bc..5084f859c0782 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -22,6 +22,7 @@ use ty::{List, TyS, ParamEnvAnd, ParamEnv}; use util::captures::Captures; use mir::interpret::{Scalar, Pointer}; +use smallvec::SmallVec; use std::iter; use std::cmp::Ordering; use rustc_target::spec::abi; @@ -1846,28 +1847,27 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { } } - /// Returns the regions directly referenced from this type (but - /// not types reachable from this type via `walk_tys`). This - /// ignores late-bound regions binders. - pub fn regions(&self) -> Vec> { + /// Push onto `out` the regions directly referenced from this type (but not + /// types reachable from this type via `walk_tys`). This ignores late-bound + /// regions binders. + pub fn push_regions(&self, out: &mut SmallVec<[ty::Region<'tcx>; 4]>) { match self.sty { Ref(region, _, _) => { - vec![region] + out.push(region); } Dynamic(ref obj, region) => { - let mut v = vec![region]; - v.extend(obj.principal().skip_binder().substs.regions()); - v + out.push(region); + out.extend(obj.principal().skip_binder().substs.regions()); } Adt(_, substs) | Opaque(_, substs) => { - substs.regions().collect() + out.extend(substs.regions()) } Closure(_, ClosureSubsts { ref substs }) | Generator(_, GeneratorSubsts { ref substs }, _) => { - substs.regions().collect() + out.extend(substs.regions()) } Projection(ref data) | UnnormalizedProjection(ref data) => { - data.substs.regions().collect() + out.extend(data.substs.regions()) } FnDef(..) | FnPtr(_) | @@ -1887,9 +1887,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { Param(_) | Bound(..) | Infer(_) | - Error => { - vec![] - } + Error => {} } } diff --git a/src/librustc_mir/transform/cleanup_post_borrowck.rs b/src/librustc_mir/transform/cleanup_post_borrowck.rs index 4d3b422ab2817..98311444e2871 100644 --- a/src/librustc_mir/transform/cleanup_post_borrowck.rs +++ b/src/librustc_mir/transform/cleanup_post_borrowck.rs @@ -37,6 +37,7 @@ use rustc::mir::{BasicBlock, FakeReadCause, Local, Location, Mir, Place}; use rustc::mir::{Rvalue, Statement, StatementKind}; use rustc::mir::visit::{MutVisitor, Visitor, TyContext}; use rustc::ty::{Ty, RegionKind, TyCtxt}; +use smallvec::smallvec; use transform::{MirPass, MirSource}; pub struct CleanEndRegions; @@ -80,7 +81,11 @@ impl<'tcx> Visitor<'tcx> for GatherBorrowedRegions { fn visit_ty(&mut self, ty: &Ty<'tcx>, _: TyContext) { // Gather regions that occur in types - for re in ty.walk().flat_map(|t| t.regions()) { + let mut regions = smallvec![]; + for t in ty.walk() { + t.push_regions(&mut regions); + } + for re in regions { match *re { RegionKind::ReScope(ce) => { self.seen_regions.insert(ce); } _ => {}, diff --git a/src/librustc_traits/implied_outlives_bounds.rs b/src/librustc_traits/implied_outlives_bounds.rs index 7cc064f9c3d3d..7514c2c18e7ca 100644 --- a/src/librustc_traits/implied_outlives_bounds.rs +++ b/src/librustc_traits/implied_outlives_bounds.rs @@ -20,6 +20,7 @@ use rustc::ty::{self, Ty, TyCtxt, TypeFoldable}; use rustc::ty::outlives::Component; use rustc::ty::query::Providers; use rustc::ty::wf; +use smallvec::{SmallVec, smallvec}; use syntax::ast::DUMMY_NODE_ID; use syntax::source_map::DUMMY_SP; use rustc::traits::FulfillmentContext; @@ -133,7 +134,8 @@ fn compute_implied_outlives_bounds<'tcx>( None => vec![], Some(ty::OutlivesPredicate(ty_a, r_b)) => { let ty_a = infcx.resolve_type_vars_if_possible(&ty_a); - let components = tcx.outlives_components(ty_a); + let mut components = smallvec![]; + tcx.push_outlives_components(ty_a, &mut components); implied_bounds_from_components(r_b, components) } }, @@ -155,7 +157,7 @@ fn compute_implied_outlives_bounds<'tcx>( /// those relationships. fn implied_bounds_from_components( sub_region: ty::Region<'tcx>, - sup_components: Vec>, + sup_components: SmallVec<[Component<'tcx>; 4]>, ) -> Vec> { sup_components .into_iter() diff --git a/src/librustc_typeck/outlives/utils.rs b/src/librustc_typeck/outlives/utils.rs index d748d93d8988e..6ed59837eb49a 100644 --- a/src/librustc_typeck/outlives/utils.rs +++ b/src/librustc_typeck/outlives/utils.rs @@ -11,6 +11,7 @@ use rustc::ty::outlives::Component; use rustc::ty::subst::{Kind, UnpackedKind}; use rustc::ty::{self, Region, RegionKind, Ty, TyCtxt}; +use smallvec::smallvec; use std::collections::BTreeSet; /// Tracks the `T: 'a` or `'a: 'a` predicates that we have inferred @@ -40,7 +41,9 @@ pub fn insert_outlives_predicate<'tcx>( // // Or if within `struct Foo` you had `T = Vec`, then // we would want to add `U: 'outlived_region` - for component in tcx.outlives_components(ty) { + let mut components = smallvec![]; + tcx.push_outlives_components(ty, &mut components); + for component in components { match component { Component::Region(r) => { // This would arise from something like: From 317f494c72aead3fecd73788569983fd2f8ea8a3 Mon Sep 17 00:00:00 2001 From: Murarth Date: Wed, 7 Nov 2018 10:59:25 -0700 Subject: [PATCH 08/26] Fix Rc/Arc allocation layout * Rounds allocation layout up to a multiple of alignment * Adds a convenience method `Layout::pad_to_align` to perform rounding --- src/liballoc/rc.rs | 6 ++++-- src/liballoc/sync.rs | 6 ++++-- src/libcore/alloc.rs | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 45f035ad04f8f..bb52d7990ff57 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -672,14 +672,16 @@ impl Rc { // Previously, layout was calculated on the expression // `&*(ptr as *const RcBox)`, but this created a misaligned // reference (see #54908). - let (layout, _) = Layout::new::>() - .extend(Layout::for_value(&*ptr)).unwrap(); + let layout = Layout::new::>() + .extend(Layout::for_value(&*ptr)).unwrap().0 + .pad_to_align().unwrap(); let mem = Global.alloc(layout) .unwrap_or_else(|_| handle_alloc_error(layout)); // Initialize the RcBox let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut RcBox; + debug_assert_eq!(Layout::for_value(&*inner), layout); ptr::write(&mut (*inner).strong, Cell::new(1)); ptr::write(&mut (*inner).weak, Cell::new(1)); diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 2c396b3b06bda..b63b3684964bb 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -575,14 +575,16 @@ impl Arc { // Previously, layout was calculated on the expression // `&*(ptr as *const ArcInner)`, but this created a misaligned // reference (see #54908). - let (layout, _) = Layout::new::>() - .extend(Layout::for_value(&*ptr)).unwrap(); + let layout = Layout::new::>() + .extend(Layout::for_value(&*ptr)).unwrap().0 + .pad_to_align().unwrap(); let mem = Global.alloc(layout) .unwrap_or_else(|_| handle_alloc_error(layout)); // Initialize the ArcInner let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut ArcInner; + debug_assert_eq!(Layout::for_value(&*inner), layout); ptr::write(&mut (*inner).strong, atomic::AtomicUsize::new(1)); ptr::write(&mut (*inner).weak, atomic::AtomicUsize::new(1)); diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 113a85abecbef..dd3e8da18a966 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -218,6 +218,23 @@ impl Layout { len_rounded_up.wrapping_sub(len) } + /// Creates a layout by rounding the size of this layout up to a multiple + /// of the layout's alignment. + /// + /// Returns `Err` if the padded size would overflow. + /// + /// This is equivalent to adding the result of `padding_needed_for` + /// to the layout's current size. + #[unstable(feature = "alloc_layout_extra", issue = "55724")] + #[inline] + pub fn pad_to_align(&self) -> Result { + let pad = self.padding_needed_for(self.align()); + let new_size = self.size().checked_add(pad) + .ok_or(LayoutErr { private: () })?; + + Layout::from_size_align(new_size, self.align()) + } + /// Creates a layout describing the record for `n` instances of /// `self`, with a suitable amount of padding between each to /// ensure that each instance is given its requested size and From df10965dc019fdbea1a302ed1a0a8fb358f3c388 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 8 Nov 2018 20:15:13 +0100 Subject: [PATCH 09/26] Prevent ICE in const-prop array oob check --- src/librustc_mir/transform/const_prop.rs | 16 ++++++++-------- src/test/ui/consts/const-prop-ice.rs | 3 +++ src/test/ui/consts/const-prop-ice.stderr | 10 ++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 src/test/ui/consts/const-prop-ice.rs create mode 100644 src/test/ui/consts/const-prop-ice.stderr diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 4f92ba400481b..885d70dc4304d 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -591,8 +591,8 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> { if let TerminatorKind::Assert { expected, msg, cond, .. } = kind { if let Some(value) = self.eval_operand(cond, source_info) { trace!("assertion on {:?} should be {:?}", value, expected); - let expected = Immediate::Scalar(Scalar::from_bool(*expected).into()); - if expected != value.0.to_immediate() { + let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected)); + if expected != self.ecx.read_scalar(value.0).unwrap() { // poison all places this operand references so that further code // doesn't use the invalid value match cond { @@ -628,20 +628,20 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> { let len = self .eval_operand(len, source_info) .expect("len must be const"); - let len = match len.0.to_immediate() { - Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits { + let len = match self.ecx.read_scalar(len.0) { + Ok(ScalarMaybeUndef::Scalar(Scalar::Bits { bits, .. })) => bits, - _ => bug!("const len not primitive: {:?}", len), + other => bug!("const len not primitive: {:?}", other), }; let index = self .eval_operand(index, source_info) .expect("index must be const"); - let index = match index.0.to_immediate() { - Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits { + let index = match self.ecx.read_scalar(index.0) { + Ok(ScalarMaybeUndef::Scalar(Scalar::Bits { bits, .. })) => bits, - _ => bug!("const index not primitive: {:?}", index), + other => bug!("const index not primitive: {:?}", other), }; format!( "index out of bounds: \ diff --git a/src/test/ui/consts/const-prop-ice.rs b/src/test/ui/consts/const-prop-ice.rs new file mode 100644 index 0000000000000..17880adc7af2f --- /dev/null +++ b/src/test/ui/consts/const-prop-ice.rs @@ -0,0 +1,3 @@ +fn main() { + [0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3 +} \ No newline at end of file diff --git a/src/test/ui/consts/const-prop-ice.stderr b/src/test/ui/consts/const-prop-ice.stderr new file mode 100644 index 0000000000000..749ef952b5ddb --- /dev/null +++ b/src/test/ui/consts/const-prop-ice.stderr @@ -0,0 +1,10 @@ +error: index out of bounds: the len is 3 but the index is 3 + --> $DIR/const-prop-ice.rs:2:5 + | +LL | [0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3 + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: #[deny(const_err)] on by default + +error: aborting due to previous error + From ffa7ce42901b7206f06de735986a6e009e6c1c5b Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 8 Nov 2018 20:18:26 +0100 Subject: [PATCH 10/26] Add more regression tests --- src/test/ui/consts/const-prop-ice2.rs | 5 +++++ src/test/ui/consts/const-prop-ice2.stderr | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/test/ui/consts/const-prop-ice2.rs create mode 100644 src/test/ui/consts/const-prop-ice2.stderr diff --git a/src/test/ui/consts/const-prop-ice2.rs b/src/test/ui/consts/const-prop-ice2.rs new file mode 100644 index 0000000000000..23bdda2cba728 --- /dev/null +++ b/src/test/ui/consts/const-prop-ice2.rs @@ -0,0 +1,5 @@ +fn main() { + enum Enum { One=1 } + let xs=[0;1 as usize]; + println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1 +} \ No newline at end of file diff --git a/src/test/ui/consts/const-prop-ice2.stderr b/src/test/ui/consts/const-prop-ice2.stderr new file mode 100644 index 0000000000000..4febd0ee1e391 --- /dev/null +++ b/src/test/ui/consts/const-prop-ice2.stderr @@ -0,0 +1,10 @@ +error: index out of bounds: the len is 1 but the index is 1 + --> $DIR/const-prop-ice2.rs:4:20 + | +LL | println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1 + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: #[deny(const_err)] on by default + +error: aborting due to previous error + From 918f70f628e9c225eb42c92b9acf2a999b34f6ee Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 9 Nov 2018 00:03:17 +0100 Subject: [PATCH 11/26] Removed an unneeded instance of `// revisions`; the compare-mode NLL shows the output is identical now. --- .../lint/lint-unused-mut-variables.nll.stderr | 150 ------------------ src/test/ui/lint/lint-unused-mut-variables.rs | 72 ++++----- ...tderr => lint-unused-mut-variables.stderr} | 32 ++-- 3 files changed, 52 insertions(+), 202 deletions(-) delete mode 100644 src/test/ui/lint/lint-unused-mut-variables.nll.stderr rename src/test/ui/lint/{lint-unused-mut-variables.lexical.stderr => lint-unused-mut-variables.stderr} (71%) diff --git a/src/test/ui/lint/lint-unused-mut-variables.nll.stderr b/src/test/ui/lint/lint-unused-mut-variables.nll.stderr deleted file mode 100644 index 40f68c6782781..0000000000000 --- a/src/test/ui/lint/lint-unused-mut-variables.nll.stderr +++ /dev/null @@ -1,150 +0,0 @@ -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:59:14 - | -LL | let x = |mut y: isize| 10; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - | -note: lint level defined here - --> $DIR/lint-unused-mut-variables.rs:19:9 - | -LL | #![deny(unused_mut)] - | ^^^^^^^^^^ - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:24:9 - | -LL | let mut a = 3; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:26:9 - | -LL | let mut a = 2; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:28:9 - | -LL | let mut b = 3; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:30:9 - | -LL | let mut a = vec![3]; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:32:10 - | -LL | let (mut a, b) = (1, 2); //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:34:9 - | -LL | let mut a; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:38:9 - | -LL | let mut b; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:47:9 - | -LL | mut x => {} //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:51:8 - | -LL | (mut x, 1) | //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:64:9 - | -LL | let mut a = &mut 5; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:69:9 - | -LL | let mut b = (&mut a,); //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:72:9 - | -LL | let mut x = &mut 1; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:84:9 - | -LL | let mut v : &mut Vec<()> = &mut vec![]; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:61:13 - | -LL | fn what(mut foo: isize) {} //[lexical]~ ERROR: variable does not need to be mutable - | ----^^^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:79:20 - | -LL | fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] { - | ----^^^ - | | - | help: remove this `mut` - -error: variable does not need to be mutable - --> $DIR/lint-unused-mut-variables.rs:143:9 - | -LL | let mut b = vec![2]; //[lexical]~ ERROR: variable does not need to be mutable - | ----^ - | | - | help: remove this `mut` - | -note: lint level defined here - --> $DIR/lint-unused-mut-variables.rs:139:8 - | -LL | #[deny(unused_mut)] - | ^^^^^^^^^^ - -error: aborting due to 17 previous errors - diff --git a/src/test/ui/lint/lint-unused-mut-variables.rs b/src/test/ui/lint/lint-unused-mut-variables.rs index 14d836074dca3..a2005ba9f72e0 100644 --- a/src/test/ui/lint/lint-unused-mut-variables.rs +++ b/src/test/ui/lint/lint-unused-mut-variables.rs @@ -8,8 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// revisions: lexical nll -#![cfg_attr(nll, feature(nll))] + + // Exercise the unused_mut attribute in some positive and negative cases @@ -21,22 +21,22 @@ fn main() { // negative cases - let mut a = 3; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable - let mut a = 2; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable - let mut b = 3; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable - let mut a = vec![3]; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable - let (mut a, b) = (1, 2); //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable - let mut a; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + let mut a = 3; //~ ERROR: variable does not need to be mutable + + let mut a = 2; //~ ERROR: variable does not need to be mutable + + let mut b = 3; //~ ERROR: variable does not need to be mutable + + let mut a = vec![3]; //~ ERROR: variable does not need to be mutable + + let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable + + let mut a; //~ ERROR: variable does not need to be mutable + a = 3; - let mut b; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + let mut b; //~ ERROR: variable does not need to be mutable + if true { b = 3; } else { @@ -44,45 +44,45 @@ fn main() { } match 30 { - mut x => {} //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + mut x => {} //~ ERROR: variable does not need to be mutable + } match (30, 2) { - (mut x, 1) | //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + (mut x, 1) | //~ ERROR: variable does not need to be mutable + (mut x, 2) | (mut x, 3) => { } _ => {} } - let x = |mut y: isize| 10; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable - fn what(mut foo: isize) {} //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + let x = |mut y: isize| 10; //~ ERROR: variable does not need to be mutable + + fn what(mut foo: isize) {} //~ ERROR: variable does not need to be mutable + + + let mut a = &mut 5; //~ ERROR: variable does not need to be mutable - let mut a = &mut 5; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable *a = 4; let mut a = 5; - let mut b = (&mut a,); //[lexical]~ ERROR: variable does not need to be mutable - *b.0 = 4; //[nll]~^ ERROR: variable does not need to be mutable + let mut b = (&mut a,); //~ ERROR: variable does not need to be mutable + *b.0 = 4; + + let mut x = &mut 1; //~ ERROR: variable does not need to be mutable - let mut x = &mut 1; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable let mut f = || { *x += 1; }; f(); fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] { - &mut arg[..] //[lexical]~^ ERROR: variable does not need to be mutable - //[nll]~^^ ERROR: variable does not need to be mutable + &mut arg[..] //~^ ERROR: variable does not need to be mutable + } - let mut v : &mut Vec<()> = &mut vec![]; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + let mut v : &mut Vec<()> = &mut vec![]; //~ ERROR: variable does not need to be mutable + v.push(()); // positive cases @@ -140,6 +140,6 @@ fn foo(mut a: isize) { fn bar() { #[allow(unused_mut)] let mut a = 3; - let mut b = vec![2]; //[lexical]~ ERROR: variable does not need to be mutable - //[nll]~^ ERROR: variable does not need to be mutable + let mut b = vec![2]; //~ ERROR: variable does not need to be mutable + } diff --git a/src/test/ui/lint/lint-unused-mut-variables.lexical.stderr b/src/test/ui/lint/lint-unused-mut-variables.stderr similarity index 71% rename from src/test/ui/lint/lint-unused-mut-variables.lexical.stderr rename to src/test/ui/lint/lint-unused-mut-variables.stderr index 40f68c6782781..60e8400c42870 100644 --- a/src/test/ui/lint/lint-unused-mut-variables.lexical.stderr +++ b/src/test/ui/lint/lint-unused-mut-variables.stderr @@ -1,7 +1,7 @@ error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:59:14 | -LL | let x = |mut y: isize| 10; //[lexical]~ ERROR: variable does not need to be mutable +LL | let x = |mut y: isize| 10; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -15,7 +15,7 @@ LL | #![deny(unused_mut)] error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:24:9 | -LL | let mut a = 3; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut a = 3; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -23,7 +23,7 @@ LL | let mut a = 3; //[lexical]~ ERROR: variable does not need to be mutable error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:26:9 | -LL | let mut a = 2; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut a = 2; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -31,7 +31,7 @@ LL | let mut a = 2; //[lexical]~ ERROR: variable does not need to be mutable error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:28:9 | -LL | let mut b = 3; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut b = 3; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -39,7 +39,7 @@ LL | let mut b = 3; //[lexical]~ ERROR: variable does not need to be mutable error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:30:9 | -LL | let mut a = vec![3]; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut a = vec![3]; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -47,7 +47,7 @@ LL | let mut a = vec![3]; //[lexical]~ ERROR: variable does not need to be m error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:32:10 | -LL | let (mut a, b) = (1, 2); //[lexical]~ ERROR: variable does not need to be mutable +LL | let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -55,7 +55,7 @@ LL | let (mut a, b) = (1, 2); //[lexical]~ ERROR: variable does not need to error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:34:9 | -LL | let mut a; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut a; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -63,7 +63,7 @@ LL | let mut a; //[lexical]~ ERROR: variable does not need to be mutable error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:38:9 | -LL | let mut b; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut b; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -71,7 +71,7 @@ LL | let mut b; //[lexical]~ ERROR: variable does not need to be mutable error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:47:9 | -LL | mut x => {} //[lexical]~ ERROR: variable does not need to be mutable +LL | mut x => {} //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -79,7 +79,7 @@ LL | mut x => {} //[lexical]~ ERROR: variable does not need to be mutabl error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:51:8 | -LL | (mut x, 1) | //[lexical]~ ERROR: variable does not need to be mutable +LL | (mut x, 1) | //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -87,7 +87,7 @@ LL | (mut x, 1) | //[lexical]~ ERROR: variable does not need to be mutable error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:64:9 | -LL | let mut a = &mut 5; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut a = &mut 5; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -95,7 +95,7 @@ LL | let mut a = &mut 5; //[lexical]~ ERROR: variable does not need to be mu error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:69:9 | -LL | let mut b = (&mut a,); //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut b = (&mut a,); //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -103,7 +103,7 @@ LL | let mut b = (&mut a,); //[lexical]~ ERROR: variable does not need to be error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:72:9 | -LL | let mut x = &mut 1; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut x = &mut 1; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -111,7 +111,7 @@ LL | let mut x = &mut 1; //[lexical]~ ERROR: variable does not need to be mu error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:84:9 | -LL | let mut v : &mut Vec<()> = &mut vec![]; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut v : &mut Vec<()> = &mut vec![]; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` @@ -119,7 +119,7 @@ LL | let mut v : &mut Vec<()> = &mut vec![]; //[lexical]~ ERROR: variable do error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:61:13 | -LL | fn what(mut foo: isize) {} //[lexical]~ ERROR: variable does not need to be mutable +LL | fn what(mut foo: isize) {} //~ ERROR: variable does not need to be mutable | ----^^^ | | | help: remove this `mut` @@ -135,7 +135,7 @@ LL | fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] { error: variable does not need to be mutable --> $DIR/lint-unused-mut-variables.rs:143:9 | -LL | let mut b = vec![2]; //[lexical]~ ERROR: variable does not need to be mutable +LL | let mut b = vec![2]; //~ ERROR: variable does not need to be mutable | ----^ | | | help: remove this `mut` From 92ef0c4bffeae68689f5e3f8173c4ad309518409 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 9 Nov 2018 00:10:19 +0100 Subject: [PATCH 12/26] Make test robust to NLL, in sense of ensuring borrows extend to something approximating lexical scope. --- .../ui/borrowck/borrowck-box-insensitivity.rs | 23 +++++++++++-------- .../borrowck-box-insensitivity.stderr | 8 +++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.rs b/src/test/ui/borrowck/borrowck-box-insensitivity.rs index eabb8d7bca3fa..5efc414da7731 100644 --- a/src/test/ui/borrowck/borrowck-box-insensitivity.rs +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.rs @@ -63,36 +63,36 @@ fn move_after_borrow() { let _y = a.y; //~^ ERROR cannot move //~| move out of + use_imm(_x); } - fn copy_after_mut_borrow() { let mut a: Box<_> = box A { x: box 0, y: 1 }; let _x = &mut a.x; let _y = a.y; //~ ERROR cannot use + use_mut(_x); } - fn move_after_mut_borrow() { let mut a: Box<_> = box B { x: box 0, y: box 1 }; let _x = &mut a.x; let _y = a.y; //~^ ERROR cannot move //~| move out of + use_mut(_x); } - fn borrow_after_mut_borrow() { let mut a: Box<_> = box A { x: box 0, y: 1 }; let _x = &mut a.x; let _y = &a.y; //~ ERROR cannot borrow //~^ immutable borrow occurs here (via `a.y`) + use_mut(_x); } - fn mut_borrow_after_borrow() { let mut a: Box<_> = box A { x: box 0, y: 1 }; let _x = &a.x; let _y = &mut a.y; //~ ERROR cannot borrow //~^ mutable borrow occurs here (via `a.y`) + use_imm(_x); } - fn copy_after_move_nested() { let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = a.x.x; @@ -124,38 +124,38 @@ fn move_after_borrow_nested() { let _y = a.y; //~^ ERROR cannot move //~| move out of + use_imm(_x); } - fn copy_after_mut_borrow_nested() { let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = &mut a.x.x; let _y = a.y; //~ ERROR cannot use + use_mut(_x); } - fn move_after_mut_borrow_nested() { let mut a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 }; let _x = &mut a.x.x; let _y = a.y; //~^ ERROR cannot move //~| move out of + use_mut(_x); } - fn borrow_after_mut_borrow_nested() { let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = &mut a.x.x; //~^ mutable borrow occurs here let _y = &a.y; //~ ERROR cannot borrow //~^ immutable borrow occurs here + use_mut(_x); } - fn mut_borrow_after_borrow_nested() { let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = &a.x.x; //~^ immutable borrow occurs here let _y = &mut a.y; //~ ERROR cannot borrow //~^ mutable borrow occurs here + use_imm(_x); } - #[rustc_error] fn main() { copy_after_move(); @@ -180,3 +180,6 @@ fn main() { borrow_after_mut_borrow_nested(); mut_borrow_after_borrow_nested(); } + +fn use_mut(_: &mut T) { } +fn use_imm(_: &T) { } diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.stderr b/src/test/ui/borrowck/borrowck-box-insensitivity.stderr index 5bf1fc0817868..b84bae748eef9 100644 --- a/src/test/ui/borrowck/borrowck-box-insensitivity.stderr +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.stderr @@ -62,7 +62,7 @@ LL | let _x = &mut a.x; | --- mutable borrow occurs here (via `a.x`) LL | let _y = &a.y; //~ ERROR cannot borrow | ^^^ immutable borrow occurs here (via `a.y`) -LL | //~^ immutable borrow occurs here (via `a.y`) +... LL | } | - mutable borrow ends here @@ -73,7 +73,7 @@ LL | let _x = &a.x; | --- immutable borrow occurs here (via `a.x`) LL | let _y = &mut a.y; //~ ERROR cannot borrow | ^^^ mutable borrow occurs here (via `a.y`) -LL | //~^ mutable borrow occurs here (via `a.y`) +... LL | } | - immutable borrow ends here @@ -143,7 +143,7 @@ LL | let _x = &mut a.x.x; LL | //~^ mutable borrow occurs here LL | let _y = &a.y; //~ ERROR cannot borrow | ^^^ immutable borrow occurs here -LL | //~^ immutable borrow occurs here +... LL | } | - mutable borrow ends here @@ -155,7 +155,7 @@ LL | let _x = &a.x.x; LL | //~^ immutable borrow occurs here LL | let _y = &mut a.y; //~ ERROR cannot borrow | ^^^ mutable borrow occurs here -LL | //~^ mutable borrow occurs here +... LL | } | - immutable borrow ends here From 24289a050a8eeb1385f73acb7c1a6de804840d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Nov 2018 15:18:55 -0800 Subject: [PATCH 13/26] Sidestep ICE in `type_of_def_id()` when called from `return_type_impl_trait` --- src/librustc/ty/context.rs | 22 +++++++++++- src/test/ui/issues/issue-55796.rs | 22 ++++++++++++ src/test/ui/issues/issue-55796.stderr | 50 +++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/issues/issue-55796.rs create mode 100644 src/test/ui/issues/issue-55796.stderr diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 05d9d4bc37d79..da0bec80a891d 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -17,7 +17,7 @@ use session::Session; use session::config::{BorrowckMode, OutputFilenames}; use session::config::CrateType; use middle; -use hir::{TraitCandidate, HirId, ItemLocalId, Node}; +use hir::{TraitCandidate, HirId, ItemKind, ItemLocalId, Node}; use hir::def::{Def, Export}; use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; use hir::map as hir_map; @@ -1602,6 +1602,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { &self, scope_def_id: DefId, ) -> Option> { + // HACK: `type_of_def_id()` will fail on these (#55796), so return None + let node_id = self.hir.as_local_node_id(scope_def_id).unwrap(); + match self.hir.get(node_id) { + Node::Item(item) => { + match item.node { + ItemKind::Trait(..) + | ItemKind::TraitAlias(..) + | ItemKind::Mod(..) + | ItemKind::ForeignMod(..) + | ItemKind::GlobalAsm(..) + | ItemKind::ExternCrate(..) + | ItemKind::Use(..) => { + return None; + } + _ => { /* type_of_def_id() will work */ } + } + } + _ => { /* type_of_def_id() will work or panic */ } + } + let ret_ty = self.type_of(scope_def_id); match ret_ty.sty { ty::FnDef(_, _) => { diff --git a/src/test/ui/issues/issue-55796.rs b/src/test/ui/issues/issue-55796.rs new file mode 100644 index 0000000000000..b48d4a9c022f4 --- /dev/null +++ b/src/test/ui/issues/issue-55796.rs @@ -0,0 +1,22 @@ +pub trait EdgeTrait { + fn target(&self) -> N; +} + +pub trait Graph<'a> { + type Node; + type Edge: EdgeTrait; + type NodesIter: Iterator + 'a; + type EdgesIter: Iterator + 'a; + + fn nodes(&'a self) -> Self::NodesIter; + fn out_edges(&'a self, u: &Self::Node) -> Self::EdgesIter; + fn in_edges(&'a self, u: &Self::Node) -> Self::EdgesIter; + + fn out_neighbors(&'a self, u: &Self::Node) -> Box> { + Box::new(self.out_edges(u).map(|e| e.target())) + } + + fn in_neighbors(&'a self, u: &Self::Node) -> Box> { + Box::new(self.in_edges(u).map(|e| e.target())) + } +} diff --git a/src/test/ui/issues/issue-55796.stderr b/src/test/ui/issues/issue-55796.stderr new file mode 100644 index 0000000000000..60ce8293a5ceb --- /dev/null +++ b/src/test/ui/issues/issue-55796.stderr @@ -0,0 +1,50 @@ +error[E0601]: `main` function not found in crate `issue_55796` + | + = note: consider adding a `main` function to `$DIR/issue-55796.rs` + +error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements + --> $DIR/issue-55796.rs:16:9 + | +LL | Box::new(self.out_edges(u).map(|e| e.target())) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: first, the lifetime cannot outlive the lifetime 'a as defined on the trait at 5:17... + --> $DIR/issue-55796.rs:5:17 + | +LL | pub trait Graph<'a> { + | ^^ +note: ...so that the type `std::iter::Map<>::EdgesIter, [closure@$DIR/issue-55796.rs:16:40: 16:54]>` will meet its required lifetime bounds + --> $DIR/issue-55796.rs:16:9 + | +LL | Box::new(self.out_edges(u).map(|e| e.target())) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: but, the lifetime must be valid for the static lifetime... + = note: ...so that the expression is assignable: + expected std::boxed::Box<(dyn std::iter::Iterator>::Node> + 'static)> + found std::boxed::Box>::Node>> + +error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements + --> $DIR/issue-55796.rs:20:9 + | +LL | Box::new(self.in_edges(u).map(|e| e.target())) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: first, the lifetime cannot outlive the lifetime 'a as defined on the trait at 5:17... + --> $DIR/issue-55796.rs:5:17 + | +LL | pub trait Graph<'a> { + | ^^ +note: ...so that the type `std::iter::Map<>::EdgesIter, [closure@$DIR/issue-55796.rs:20:39: 20:53]>` will meet its required lifetime bounds + --> $DIR/issue-55796.rs:20:9 + | +LL | Box::new(self.in_edges(u).map(|e| e.target())) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: but, the lifetime must be valid for the static lifetime... + = note: ...so that the expression is assignable: + expected std::boxed::Box<(dyn std::iter::Iterator>::Node> + 'static)> + found std::boxed::Box>::Node>> + +error: aborting due to 3 previous errors + +Some errors occurred: E0495, E0601. +For more information about an error, try `rustc --explain E0495`. From d0151cac17cab1f88f4c3d27ac8cbb8e53f3fab6 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 9 Nov 2018 00:21:46 +0100 Subject: [PATCH 14/26] Switch to using `// revisions` to explicit encode NLL's change to `Box` treatment. --- ...err => borrowck-box-insensitivity.ast.stderr} | 0 .../borrowck-box-insensitivity.mir.stderr | 14 ++++++++++++++ .../ui/borrowck/borrowck-box-insensitivity.rs | 16 ++++++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) rename src/test/ui/borrowck/{borrowck-box-insensitivity.stderr => borrowck-box-insensitivity.ast.stderr} (100%) create mode 100644 src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.stderr b/src/test/ui/borrowck/borrowck-box-insensitivity.ast.stderr similarity index 100% rename from src/test/ui/borrowck/borrowck-box-insensitivity.stderr rename to src/test/ui/borrowck/borrowck-box-insensitivity.ast.stderr diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr b/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr new file mode 100644 index 0000000000000..0e380e90e7591 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr @@ -0,0 +1,14 @@ +error: compilation successful + --> $DIR/borrowck-box-insensitivity.rs:160:1 + | +LL | / fn main() { +LL | | copy_after_move(); +LL | | move_after_move(); +LL | | borrow_after_move(); +... | +LL | | mut_borrow_after_borrow_nested(); +LL | | } + | |_^ + +error: aborting due to previous error + diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.rs b/src/test/ui/borrowck/borrowck-box-insensitivity.rs index 5efc414da7731..1f1a7ea5ad9c1 100644 --- a/src/test/ui/borrowck/borrowck-box-insensitivity.rs +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.rs @@ -1,13 +1,13 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. +// This test is an artifact of the old policy that `Box` should not +// be treated specially by the AST-borrowck. // -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. +// NLL goes back to treating `Box` specially (namely, knowing that +// it uniquely owns the data it holds). See rust-lang/rfcs#130. +// revisions: ast mir +//[ast] compile-flags: -Z borrowck=ast +//[mir] compile-flags: -Z borrowck=mir +// ignore-compare-mode-nll #![feature(box_syntax, rustc_attrs)] struct A { From 9b6a568c6f32af27bc050b7df0dabe4c5c9aefee Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 9 Nov 2018 00:26:28 +0100 Subject: [PATCH 15/26] Fix the expected error annotations. (The commit prior to this actual passes our test suite, "thanks" to #55695. But since I am aware of that bug, I took advantage of it in choosing how to order my commit series...) --- .../borrowck-box-insensitivity.ast.stderr | 42 ++++----- .../borrowck-box-insensitivity.mir.stderr | 2 +- .../ui/borrowck/borrowck-box-insensitivity.rs | 86 +++++++++---------- 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.ast.stderr b/src/test/ui/borrowck/borrowck-box-insensitivity.ast.stderr index b84bae748eef9..95b26a5724a41 100644 --- a/src/test/ui/borrowck/borrowck-box-insensitivity.ast.stderr +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.ast.stderr @@ -3,8 +3,8 @@ error[E0382]: use of moved value: `a` | LL | let _x = a.x; | -- value moved here -LL | //~^ value moved here -LL | let _y = a.y; //~ ERROR use of moved +LL | //[ast]~^ value moved here +LL | let _y = a.y; //[ast]~ ERROR use of moved | ^^ value used here after move | = note: move occurs because `a.x` has type `std::boxed::Box`, which does not implement the `Copy` trait @@ -14,8 +14,8 @@ error[E0382]: use of moved value: `a` | LL | let _x = a.x; | -- value moved here -LL | //~^ value moved here -LL | let _y = a.y; //~ ERROR use of moved +LL | //[ast]~^ value moved here +LL | let _y = a.y; //[ast]~ ERROR use of moved | ^^ value used here after move | = note: move occurs because `a.x` has type `std::boxed::Box`, which does not implement the `Copy` trait @@ -25,8 +25,8 @@ error[E0382]: use of moved value: `a` | LL | let _x = a.x; | -- value moved here -LL | //~^ value moved here -LL | let _y = &a.y; //~ ERROR use of moved +LL | //[ast]~^ value moved here +LL | let _y = &a.y; //[ast]~ ERROR use of moved | ^^^ value used here after move | = note: move occurs because `a.x` has type `std::boxed::Box`, which does not implement the `Copy` trait @@ -44,7 +44,7 @@ error[E0503]: cannot use `a.y` because it was mutably borrowed | LL | let _x = &mut a.x; | --- borrow of `a.x` occurs here -LL | let _y = a.y; //~ ERROR cannot use +LL | let _y = a.y; //[ast]~ ERROR cannot use | ^^ use of borrowed `a.x` error[E0505]: cannot move out of `a.y` because it is borrowed @@ -60,7 +60,7 @@ error[E0502]: cannot borrow `a` (via `a.y`) as immutable because `a` is also bor | LL | let _x = &mut a.x; | --- mutable borrow occurs here (via `a.x`) -LL | let _y = &a.y; //~ ERROR cannot borrow +LL | let _y = &a.y; //[ast]~ ERROR cannot borrow | ^^^ immutable borrow occurs here (via `a.y`) ... LL | } @@ -71,7 +71,7 @@ error[E0502]: cannot borrow `a` (via `a.y`) as mutable because `a` is also borro | LL | let _x = &a.x; | --- immutable borrow occurs here (via `a.x`) -LL | let _y = &mut a.y; //~ ERROR cannot borrow +LL | let _y = &mut a.y; //[ast]~ ERROR cannot borrow | ^^^ mutable borrow occurs here (via `a.y`) ... LL | } @@ -82,8 +82,8 @@ error[E0382]: use of collaterally moved value: `a.y` | LL | let _x = a.x.x; | -- value moved here -LL | //~^ value moved here -LL | let _y = a.y; //~ ERROR use of collaterally moved +LL | //[ast]~^ value moved here +LL | let _y = a.y; //[ast]~ ERROR use of collaterally moved | ^^ value used here after move | = note: move occurs because `a.x.x` has type `std::boxed::Box`, which does not implement the `Copy` trait @@ -93,8 +93,8 @@ error[E0382]: use of collaterally moved value: `a.y` | LL | let _x = a.x.x; | -- value moved here -LL | //~^ value moved here -LL | let _y = a.y; //~ ERROR use of collaterally moved +LL | //[ast]~^ value moved here +LL | let _y = a.y; //[ast]~ ERROR use of collaterally moved | ^^ value used here after move | = note: move occurs because `a.x.x` has type `std::boxed::Box`, which does not implement the `Copy` trait @@ -104,8 +104,8 @@ error[E0382]: use of collaterally moved value: `a.y` | LL | let _x = a.x.x; | -- value moved here -LL | //~^ value moved here -LL | let _y = &a.y; //~ ERROR use of collaterally moved +LL | //[ast]~^ value moved here +LL | let _y = &a.y; //[ast]~ ERROR use of collaterally moved | ^^^ value used here after move | = note: move occurs because `a.x.x` has type `std::boxed::Box`, which does not implement the `Copy` trait @@ -115,7 +115,7 @@ error[E0505]: cannot move out of `a.y` because it is borrowed | LL | let _x = &a.x.x; | ----- borrow of `a.x.x` occurs here -LL | //~^ borrow of `a.x.x` occurs here +LL | //[ast]~^ borrow of `a.x.x` occurs here LL | let _y = a.y; | ^^ move out of `a.y` occurs here @@ -124,7 +124,7 @@ error[E0503]: cannot use `a.y` because it was mutably borrowed | LL | let _x = &mut a.x.x; | ----- borrow of `a.x.x` occurs here -LL | let _y = a.y; //~ ERROR cannot use +LL | let _y = a.y; //[ast]~ ERROR cannot use | ^^ use of borrowed `a.x.x` error[E0505]: cannot move out of `a.y` because it is borrowed @@ -140,8 +140,8 @@ error[E0502]: cannot borrow `a.y` as immutable because `a.x.x` is also borrowed | LL | let _x = &mut a.x.x; | ----- mutable borrow occurs here -LL | //~^ mutable borrow occurs here -LL | let _y = &a.y; //~ ERROR cannot borrow +LL | //[ast]~^ mutable borrow occurs here +LL | let _y = &a.y; //[ast]~ ERROR cannot borrow | ^^^ immutable borrow occurs here ... LL | } @@ -152,8 +152,8 @@ error[E0502]: cannot borrow `a.y` as mutable because `a.x.x` is also borrowed as | LL | let _x = &a.x.x; | ----- immutable borrow occurs here -LL | //~^ immutable borrow occurs here -LL | let _y = &mut a.y; //~ ERROR cannot borrow +LL | //[ast]~^ immutable borrow occurs here +LL | let _y = &mut a.y; //[ast]~ ERROR cannot borrow | ^^^ mutable borrow occurs here ... LL | } diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr b/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr index 0e380e90e7591..171e992e8a628 100644 --- a/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.mir.stderr @@ -1,7 +1,7 @@ error: compilation successful --> $DIR/borrowck-box-insensitivity.rs:160:1 | -LL | / fn main() { +LL | / fn main() { //[mir]~ ERROR compilation successful LL | | copy_after_move(); LL | | move_after_move(); LL | | borrow_after_move(); diff --git a/src/test/ui/borrowck/borrowck-box-insensitivity.rs b/src/test/ui/borrowck/borrowck-box-insensitivity.rs index 1f1a7ea5ad9c1..2af97a9fc1d58 100644 --- a/src/test/ui/borrowck/borrowck-box-insensitivity.rs +++ b/src/test/ui/borrowck/borrowck-box-insensitivity.rs @@ -33,131 +33,131 @@ struct D { fn copy_after_move() { let a: Box<_> = box A { x: box 0, y: 1 }; let _x = a.x; - //~^ value moved here - let _y = a.y; //~ ERROR use of moved - //~^ move occurs because `a.x` has type `std::boxed::Box` - //~| value used here after move + //[ast]~^ value moved here + let _y = a.y; //[ast]~ ERROR use of moved + //[ast]~^ move occurs because `a.x` has type `std::boxed::Box` + //[ast]~| value used here after move } fn move_after_move() { let a: Box<_> = box B { x: box 0, y: box 1 }; let _x = a.x; - //~^ value moved here - let _y = a.y; //~ ERROR use of moved - //~^ move occurs because `a.x` has type `std::boxed::Box` - //~| value used here after move + //[ast]~^ value moved here + let _y = a.y; //[ast]~ ERROR use of moved + //[ast]~^ move occurs because `a.x` has type `std::boxed::Box` + //[ast]~| value used here after move } fn borrow_after_move() { let a: Box<_> = box A { x: box 0, y: 1 }; let _x = a.x; - //~^ value moved here - let _y = &a.y; //~ ERROR use of moved - //~^ move occurs because `a.x` has type `std::boxed::Box` - //~| value used here after move + //[ast]~^ value moved here + let _y = &a.y; //[ast]~ ERROR use of moved + //[ast]~^ move occurs because `a.x` has type `std::boxed::Box` + //[ast]~| value used here after move } fn move_after_borrow() { let a: Box<_> = box B { x: box 0, y: box 1 }; let _x = &a.x; let _y = a.y; - //~^ ERROR cannot move - //~| move out of + //[ast]~^ ERROR cannot move + //[ast]~| move out of use_imm(_x); } fn copy_after_mut_borrow() { let mut a: Box<_> = box A { x: box 0, y: 1 }; let _x = &mut a.x; - let _y = a.y; //~ ERROR cannot use + let _y = a.y; //[ast]~ ERROR cannot use use_mut(_x); } fn move_after_mut_borrow() { let mut a: Box<_> = box B { x: box 0, y: box 1 }; let _x = &mut a.x; let _y = a.y; - //~^ ERROR cannot move - //~| move out of + //[ast]~^ ERROR cannot move + //[ast]~| move out of use_mut(_x); } fn borrow_after_mut_borrow() { let mut a: Box<_> = box A { x: box 0, y: 1 }; let _x = &mut a.x; - let _y = &a.y; //~ ERROR cannot borrow - //~^ immutable borrow occurs here (via `a.y`) + let _y = &a.y; //[ast]~ ERROR cannot borrow + //[ast]~^ immutable borrow occurs here (via `a.y`) use_mut(_x); } fn mut_borrow_after_borrow() { let mut a: Box<_> = box A { x: box 0, y: 1 }; let _x = &a.x; - let _y = &mut a.y; //~ ERROR cannot borrow - //~^ mutable borrow occurs here (via `a.y`) + let _y = &mut a.y; //[ast]~ ERROR cannot borrow + //[ast]~^ mutable borrow occurs here (via `a.y`) use_imm(_x); } fn copy_after_move_nested() { let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = a.x.x; - //~^ value moved here - let _y = a.y; //~ ERROR use of collaterally moved - //~| value used here after move + //[ast]~^ value moved here + let _y = a.y; //[ast]~ ERROR use of collaterally moved + //[ast]~| value used here after move } fn move_after_move_nested() { let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 }; let _x = a.x.x; - //~^ value moved here - let _y = a.y; //~ ERROR use of collaterally moved - //~| value used here after move + //[ast]~^ value moved here + let _y = a.y; //[ast]~ ERROR use of collaterally moved + //[ast]~| value used here after move } fn borrow_after_move_nested() { let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = a.x.x; - //~^ value moved here - let _y = &a.y; //~ ERROR use of collaterally moved - //~| value used here after move + //[ast]~^ value moved here + let _y = &a.y; //[ast]~ ERROR use of collaterally moved + //[ast]~| value used here after move } fn move_after_borrow_nested() { let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 }; let _x = &a.x.x; - //~^ borrow of `a.x.x` occurs here + //[ast]~^ borrow of `a.x.x` occurs here let _y = a.y; - //~^ ERROR cannot move - //~| move out of + //[ast]~^ ERROR cannot move + //[ast]~| move out of use_imm(_x); } fn copy_after_mut_borrow_nested() { let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = &mut a.x.x; - let _y = a.y; //~ ERROR cannot use + let _y = a.y; //[ast]~ ERROR cannot use use_mut(_x); } fn move_after_mut_borrow_nested() { let mut a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 }; let _x = &mut a.x.x; let _y = a.y; - //~^ ERROR cannot move - //~| move out of + //[ast]~^ ERROR cannot move + //[ast]~| move out of use_mut(_x); } fn borrow_after_mut_borrow_nested() { let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = &mut a.x.x; - //~^ mutable borrow occurs here - let _y = &a.y; //~ ERROR cannot borrow - //~^ immutable borrow occurs here + //[ast]~^ mutable borrow occurs here + let _y = &a.y; //[ast]~ ERROR cannot borrow + //[ast]~^ immutable borrow occurs here use_mut(_x); } fn mut_borrow_after_borrow_nested() { let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 }; let _x = &a.x.x; - //~^ immutable borrow occurs here - let _y = &mut a.y; //~ ERROR cannot borrow - //~^ mutable borrow occurs here + //[ast]~^ immutable borrow occurs here + let _y = &mut a.y; //[ast]~ ERROR cannot borrow + //[ast]~^ mutable borrow occurs here use_imm(_x); } #[rustc_error] -fn main() { +fn main() { //[mir]~ ERROR compilation successful copy_after_move(); move_after_move(); borrow_after_move(); From a66d7b2001def5ab87e99d661e31890660f38374 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 9 Nov 2018 17:12:52 +1100 Subject: [PATCH 16/26] Use `SmallVec` to avoid allocations in `from_decimal_string`. This reduces the number of allocations in a "check clean" build of `tuple-stress` by 14%, reducing instruction counts by 0.6%. --- src/Cargo.lock | 1 + src/librustc_apfloat/Cargo.toml | 1 + src/librustc_apfloat/ieee.rs | 13 +++++++------ src/librustc_apfloat/lib.rs | 1 + 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index b4317864502ce..17b1744a07f2f 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -2095,6 +2095,7 @@ version = "0.0.0" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_cratesio_shim 0.0.0", + "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/src/librustc_apfloat/Cargo.toml b/src/librustc_apfloat/Cargo.toml index 735b74f156500..a8a5da90c7a6b 100644 --- a/src/librustc_apfloat/Cargo.toml +++ b/src/librustc_apfloat/Cargo.toml @@ -10,3 +10,4 @@ path = "lib.rs" [dependencies] bitflags = "1.0" rustc_cratesio_shim = { path = "../librustc_cratesio_shim" } +smallvec = { version = "0.6.5", features = ["union"] } diff --git a/src/librustc_apfloat/ieee.rs b/src/librustc_apfloat/ieee.rs index 87d59d2e763cb..4f405858e350c 100644 --- a/src/librustc_apfloat/ieee.rs +++ b/src/librustc_apfloat/ieee.rs @@ -11,6 +11,7 @@ use {Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO}; use {Float, FloatConvert, ParseError, Round, Status, StatusAnd}; +use smallvec::{SmallVec, smallvec}; use std::cmp::{self, Ordering}; use std::convert::TryFrom; use std::fmt::{self, Write}; @@ -1962,7 +1963,7 @@ impl IeeeFloat { // to hold the full significand, and an extra limb required by // tcMultiplyPart. let max_limbs = limbs_for_bits(1 + 196 * significand_digits / 59); - let mut dec_sig = Vec::with_capacity(max_limbs); + let mut dec_sig: SmallVec<[Limb; 1]> = SmallVec::with_capacity(max_limbs); // Convert to binary efficiently - we do almost all multiplication // in a Limb. When this would overflow do we do a single @@ -2021,11 +2022,11 @@ impl IeeeFloat { const FIRST_EIGHT_POWERS: [Limb; 8] = [1, 5, 25, 125, 625, 3125, 15625, 78125]; - let mut p5_scratch = vec![]; - let mut p5 = vec![FIRST_EIGHT_POWERS[4]]; + let mut p5_scratch = smallvec![]; + let mut p5: SmallVec<[Limb; 1]> = smallvec![FIRST_EIGHT_POWERS[4]]; - let mut r_scratch = vec![]; - let mut r = vec![FIRST_EIGHT_POWERS[power & 7]]; + let mut r_scratch = smallvec![]; + let mut r: SmallVec<[Limb; 1]> = smallvec![FIRST_EIGHT_POWERS[power & 7]]; power >>= 3; while power > 0 { @@ -2064,7 +2065,7 @@ impl IeeeFloat { let calc_precision = (LIMB_BITS << attempt) - 1; attempt += 1; - let calc_normal_from_limbs = |sig: &mut Vec, + let calc_normal_from_limbs = |sig: &mut SmallVec<[Limb; 1]>, limbs: &[Limb]| -> StatusAnd { sig.resize(limbs_for_bits(calc_precision), 0); diff --git a/src/librustc_apfloat/lib.rs b/src/librustc_apfloat/lib.rs index 6ea722ba769c1..69c9f385409e4 100644 --- a/src/librustc_apfloat/lib.rs +++ b/src/librustc_apfloat/lib.rs @@ -53,6 +53,7 @@ extern crate rustc_cratesio_shim; #[macro_use] extern crate bitflags; +extern crate smallvec; use std::cmp::Ordering; use std::fmt; From 1206549d1b3dbdd694b1218516f522e71228b3b5 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 9 Nov 2018 10:11:20 +0100 Subject: [PATCH 17/26] Fix tidy --- src/test/ui/consts/const-prop-ice.rs | 2 +- src/test/ui/consts/const-prop-ice2.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/consts/const-prop-ice.rs b/src/test/ui/consts/const-prop-ice.rs index 17880adc7af2f..13309f978b672 100644 --- a/src/test/ui/consts/const-prop-ice.rs +++ b/src/test/ui/consts/const-prop-ice.rs @@ -1,3 +1,3 @@ fn main() { [0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3 -} \ No newline at end of file +} diff --git a/src/test/ui/consts/const-prop-ice2.rs b/src/test/ui/consts/const-prop-ice2.rs index 23bdda2cba728..e5fd79f11676e 100644 --- a/src/test/ui/consts/const-prop-ice2.rs +++ b/src/test/ui/consts/const-prop-ice2.rs @@ -2,4 +2,4 @@ fn main() { enum Enum { One=1 } let xs=[0;1 as usize]; println!("{}", xs[Enum::One as usize]); //~ ERROR the len is 1 but the index is 1 -} \ No newline at end of file +} From 5f91373c778da7d28306d0faba590a22612c281b Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 9 Nov 2018 12:06:12 +0100 Subject: [PATCH 18/26] Typecheck patterns of all match arms first, so we get types for bindings. --- src/librustc_typeck/check/_match.rs | 8 +++---- ...10-must-typeck-match-pats-before-guards.rs | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/typeck/issue-55810-must-typeck-match-pats-before-guards.rs diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index 4a300fe09215c..a477df6ae2d56 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -626,9 +626,9 @@ https://doc.rust-lang.org/reference/types.html#trait-objects"); let discrim_diverges = self.diverges.get(); self.diverges.set(Diverges::Maybe); - // Typecheck the patterns first, so that we get types for all the - // bindings. - let all_arm_pats_diverge = arms.iter().map(|arm| { + // rust-lang/rust#55810: Typecheck patterns first (via eager + // collection into `Vec`), so we get types for all bindings. + let all_arm_pats_diverge: Vec<_> = arms.iter().map(|arm| { let mut all_pats_diverge = Diverges::WarnedAlways; for p in &arm.pats { self.diverges.set(Diverges::Maybe); @@ -644,7 +644,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects"); Diverges::Maybe => Diverges::Maybe, Diverges::Always | Diverges::WarnedAlways => Diverges::WarnedAlways, } - }); + }).collect(); // Now typecheck the blocks. // diff --git a/src/test/ui/typeck/issue-55810-must-typeck-match-pats-before-guards.rs b/src/test/ui/typeck/issue-55810-must-typeck-match-pats-before-guards.rs new file mode 100644 index 0000000000000..9eed80ad886e0 --- /dev/null +++ b/src/test/ui/typeck/issue-55810-must-typeck-match-pats-before-guards.rs @@ -0,0 +1,23 @@ +// compile-pass + +// rust-lang/rust#55810: types for a binding in a match arm can be +// inferred from arms that come later in the match. + +struct S; + +impl S { + fn method(&self) -> bool { + unimplemented!() + } +} + +fn get() -> T { + unimplemented!() +} + +fn main() { + match get() { + x if x.method() => {} + &S => {} + } +} From e6a466e60c4660d8a74050189286e258918311e6 Mon Sep 17 00:00:00 2001 From: David Wood Date: Fri, 9 Nov 2018 15:39:32 +0100 Subject: [PATCH 19/26] Fix ICE and find correct return span. This commit fixes an ICE and determines the correct return span in cases with a method implemented on a struct with an an elided lifetime. --- .../error_reporting/region_name.rs | 34 ++++++++++--------- src/test/ui/nll/issue-55394.rs | 25 ++++++++++++++ src/test/ui/nll/issue-55394.stderr | 12 +++++++ 3 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/nll/issue-55394.rs create mode 100644 src/test/ui/nll/issue-55394.stderr diff --git a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs index 99372a511a9de..a32fb0503a814 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs @@ -687,22 +687,24 @@ impl<'tcx> RegionInferenceContext<'tcx> { let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir"); - let (return_span, mir_description) = - if let hir::ExprKind::Closure(_, _, _, span, gen_move) = - tcx.hir.expect_expr(mir_node_id).node - { - ( - tcx.sess.source_map().end_point(span), - if gen_move.is_some() { - " of generator" - } else { - " of closure" - }, - ) - } else { - // unreachable? - (mir.span, "") - }; + let (return_span, mir_description) = match tcx.hir.get(mir_node_id) { + hir::Node::Expr(hir::Expr { + node: hir::ExprKind::Closure(_, _, _, span, gen_move), + .. + }) => ( + tcx.sess.source_map().end_point(*span), + if gen_move.is_some() { + " of generator" + } else { + " of closure" + }, + ), + hir::Node::ImplItem(hir::ImplItem { + node: hir::ImplItemKind::Method(method_sig, _), + .. + }) => (method_sig.decl.output.span(), ""), + _ => (mir.span, ""), + }; Some(RegionName { // This counter value will already have been used, so this function will increment it diff --git a/src/test/ui/nll/issue-55394.rs b/src/test/ui/nll/issue-55394.rs new file mode 100644 index 0000000000000..452fc88d1ecdd --- /dev/null +++ b/src/test/ui/nll/issue-55394.rs @@ -0,0 +1,25 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(nll)] + +struct Bar; + +struct Foo<'s> { + bar: &'s mut Bar, +} + +impl Foo<'_> { + fn new(bar: &mut Bar) -> Self { + Foo { bar } + } +} + +fn main() { } diff --git a/src/test/ui/nll/issue-55394.stderr b/src/test/ui/nll/issue-55394.stderr new file mode 100644 index 0000000000000..284d7afa6fd23 --- /dev/null +++ b/src/test/ui/nll/issue-55394.stderr @@ -0,0 +1,12 @@ +error: unsatisfied lifetime constraints + --> $DIR/issue-55394.rs:21:9 + | +LL | fn new(bar: &mut Bar) -> Self { + | - ---- return type is Foo<'2> + | | + | let's call the lifetime of this reference `'1` +LL | Foo { bar } + | ^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` + +error: aborting due to previous error + From f4c9dd55d6446f6308dc70ab8f88129a87c5daf1 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 9 Nov 2018 17:55:24 +0100 Subject: [PATCH 20/26] Add missing `rustc_promotable` attribute to unsigned `min_value` and `max_value` --- src/libcore/num/mod.rs | 2 ++ .../auxiliary/promotable_const_fn_lib.rs | 31 +++++++++++++++++++ src/test/ui/consts/promote_fn_calls.rs | 13 ++++++++ src/test/ui/consts/promote_fn_calls_std.rs | 30 ++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 src/test/ui/consts/auxiliary/promotable_const_fn_lib.rs create mode 100644 src/test/ui/consts/promote_fn_calls.rs create mode 100644 src/test/ui/consts/promote_fn_calls_std.rs diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index c6cbeea5a0ea6..b71b8e7418dc7 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2152,6 +2152,7 @@ Basic usage: ", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), 0);", $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_promotable] #[inline] pub const fn min_value() -> Self { 0 } } @@ -2168,6 +2169,7 @@ Basic usage: stringify!($MaxV), ");", $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_promotable] #[inline] pub const fn max_value() -> Self { !0 } } diff --git a/src/test/ui/consts/auxiliary/promotable_const_fn_lib.rs b/src/test/ui/consts/auxiliary/promotable_const_fn_lib.rs new file mode 100644 index 0000000000000..f6bbcc60e4e64 --- /dev/null +++ b/src/test/ui/consts/auxiliary/promotable_const_fn_lib.rs @@ -0,0 +1,31 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Crate that exports a const fn. Used for testing cross-crate. + +#![feature(staged_api, rustc_attrs)] +#![stable(since="1.0.0", feature = "mep")] + +#![crate_type="rlib"] + +#[rustc_promotable] +#[stable(since="1.0.0", feature = "mep")] +#[inline] +pub const fn foo() -> usize { 22 } + +#[stable(since="1.0.0", feature = "mep")] +pub struct Foo(usize); + +impl Foo { + #[stable(since="1.0.0", feature = "mep")] + #[inline] + #[rustc_promotable] + pub const fn foo() -> usize { 22 } +} diff --git a/src/test/ui/consts/promote_fn_calls.rs b/src/test/ui/consts/promote_fn_calls.rs new file mode 100644 index 0000000000000..045322de34708 --- /dev/null +++ b/src/test/ui/consts/promote_fn_calls.rs @@ -0,0 +1,13 @@ +// compile-pass +// aux-build:promotable_const_fn_lib.rs + +#![feature(nll)] + +extern crate promotable_const_fn_lib; + +use promotable_const_fn_lib::{foo, Foo}; + +fn main() { + let x: &'static usize = &foo(); + let x: &'static usize = &Foo::foo(); +} diff --git a/src/test/ui/consts/promote_fn_calls_std.rs b/src/test/ui/consts/promote_fn_calls_std.rs new file mode 100644 index 0000000000000..0350708d673d7 --- /dev/null +++ b/src/test/ui/consts/promote_fn_calls_std.rs @@ -0,0 +1,30 @@ +// compile-pass + +#![feature(nll)] + +fn main() { + let x: &'static u8 = &u8::max_value(); + let x: &'static u16 = &u16::max_value(); + let x: &'static u32 = &u32::max_value(); + let x: &'static u64 = &u64::max_value(); + let x: &'static u128 = &u128::max_value(); + let x: &'static usize = &usize::max_value(); + let x: &'static u8 = &u8::min_value(); + let x: &'static u16 = &u16::min_value(); + let x: &'static u32 = &u32::min_value(); + let x: &'static u64 = &u64::min_value(); + let x: &'static u128 = &u128::min_value(); + let x: &'static usize = &usize::min_value(); + let x: &'static i8 = &i8::max_value(); + let x: &'static i16 = &i16::max_value(); + let x: &'static i32 = &i32::max_value(); + let x: &'static i64 = &i64::max_value(); + let x: &'static i128 = &i128::max_value(); + let x: &'static isize = &isize::max_value(); + let x: &'static i8 = &i8::min_value(); + let x: &'static i16 = &i16::min_value(); + let x: &'static i32 = &i32::min_value(); + let x: &'static i64 = &i64::min_value(); + let x: &'static i128 = &i128::min_value(); + let x: &'static isize = &isize::min_value(); +} From a90240d2791b2eaa4ae1401a1b7e280f0da4c524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 9 Nov 2018 10:16:07 -0800 Subject: [PATCH 21/26] Simplify logic --- src/librustc/ty/context.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index da0bec80a891d..82095a2f5b01d 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1607,16 +1607,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { match self.hir.get(node_id) { Node::Item(item) => { match item.node { - ItemKind::Trait(..) - | ItemKind::TraitAlias(..) - | ItemKind::Mod(..) - | ItemKind::ForeignMod(..) - | ItemKind::GlobalAsm(..) - | ItemKind::ExternCrate(..) - | ItemKind::Use(..) => { + ItemKind::Fn(..) => { /* type_of_def_id() will work */ } + _ => { return None; } - _ => { /* type_of_def_id() will work */ } } } _ => { /* type_of_def_id() will work or panic */ } From 3cce5c79778088a26f6099f293256d5d7834fdb3 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Tue, 6 Nov 2018 22:31:09 -0500 Subject: [PATCH 22/26] Don't inline virtual calls (take 2) When I fixed the previous mis-optimizations, I didn't realize there were actually two different places where we mutate `callsites` and both of them should have the same behavior. As a result, if a function was inlined and that function contained virtual function calls, they were incorrectly being inlined. I also added a test case which covers this. --- src/librustc_mir/transform/inline.rs | 94 ++++++++++++----------- src/test/mir-opt/inline-trait-method_2.rs | 36 +++++++++ 2 files changed, 85 insertions(+), 45 deletions(-) create mode 100644 src/test/mir-opt/inline-trait-method_2.rs diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 2db3bbda3233b..1cce0de5152fd 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -19,7 +19,7 @@ use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use rustc::mir::*; use rustc::mir::visit::*; -use rustc::ty::{self, Instance, InstanceDef, Ty, TyCtxt}; +use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt}; use rustc::ty::subst::{Subst,Substs}; use std::collections::VecDeque; @@ -85,39 +85,16 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> { // Only do inlining into fn bodies. let id = self.tcx.hir.as_local_node_id(self.source.def_id).unwrap(); let body_owner_kind = self.tcx.hir.body_owner_kind(id); + if let (hir::BodyOwnerKind::Fn, None) = (body_owner_kind, self.source.promoted) { for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated() { - // Don't inline calls that are in cleanup blocks. - if bb_data.is_cleanup { continue; } - - // Only consider direct calls to functions - let terminator = bb_data.terminator(); - if let TerminatorKind::Call { - func: ref op, .. } = terminator.kind { - if let ty::FnDef(callee_def_id, substs) = op.ty(caller_mir, self.tcx).sty { - if let Some(instance) = Instance::resolve(self.tcx, - param_env, - callee_def_id, - substs) { - let is_virtual = - if let InstanceDef::Virtual(..) = instance.def { - true - } else { - false - }; - - if !is_virtual { - callsites.push_back(CallSite { - callee: instance.def_id(), - substs: instance.substs, - bb, - location: terminator.source_info - }); - } - } - } - } + if let Some(callsite) = self.get_valid_function_call(bb, + bb_data, + caller_mir, + param_env) { + callsites.push_back(callsite); + } } } else { return; @@ -163,20 +140,13 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> { // Add callsites from inlined function for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated().skip(start) { - // Only consider direct calls to functions - let terminator = bb_data.terminator(); - if let TerminatorKind::Call { - func: Operand::Constant(ref f), .. } = terminator.kind { - if let ty::FnDef(callee_def_id, substs) = f.ty.sty { - // Don't inline the same function multiple times. - if callsite.callee != callee_def_id { - callsites.push_back(CallSite { - callee: callee_def_id, - substs, - bb, - location: terminator.source_info - }); - } + if let Some(new_callsite) = self.get_valid_function_call(bb, + bb_data, + caller_mir, + param_env) { + // Don't inline the same function multiple times. + if callsite.callee != new_callsite.callee { + callsites.push_back(new_callsite); } } } @@ -198,6 +168,40 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> { } } + fn get_valid_function_call(&self, + bb: BasicBlock, + bb_data: &BasicBlockData<'tcx>, + caller_mir: &Mir<'tcx>, + param_env: ParamEnv<'tcx>, + ) -> Option> { + // Don't inline calls that are in cleanup blocks. + if bb_data.is_cleanup { return None; } + + // Only consider direct calls to functions + let terminator = bb_data.terminator(); + if let TerminatorKind::Call { func: ref op, .. } = terminator.kind { + if let ty::FnDef(callee_def_id, substs) = op.ty(caller_mir, self.tcx).sty { + let instance = Instance::resolve(self.tcx, + param_env, + callee_def_id, + substs)?; + + if let InstanceDef::Virtual(..) = instance.def { + return None; + } + + return Some(CallSite { + callee: instance.def_id(), + substs: instance.substs, + bb, + location: terminator.source_info + }); + } + } + + None + } + fn consider_optimizing(&self, callsite: CallSite<'tcx>, callee_mir: &Mir<'tcx>) diff --git a/src/test/mir-opt/inline-trait-method_2.rs b/src/test/mir-opt/inline-trait-method_2.rs new file mode 100644 index 0000000000000..aa756f4a23370 --- /dev/null +++ b/src/test/mir-opt/inline-trait-method_2.rs @@ -0,0 +1,36 @@ +// compile-flags: -Z span_free_formats -Z mir-opt-level=3 + +#[inline] +fn test(x: &dyn X) -> bool { + x.y() +} + +fn test2(x: &dyn X) -> bool { + test(x) +} + +trait X { + fn y(&self) -> bool { + false + } +} + +impl X for () { + fn y(&self) -> bool { + true + } +} + +fn main() { + println!("Should be true: {}", test2(&())); +} + +// END RUST SOURCE +// START rustc.test2.Inline.after.mir +// ... +// bb0: { +// ... +// _0 = const X::y(move _2) -> bb1; +// } +// ... +// END rustc.test2.Inline.after.mir From 38d2f9b470f26ab282322f35c1494cfd37ab9354 Mon Sep 17 00:00:00 2001 From: Daniel Alley Date: Fri, 9 Nov 2018 23:12:46 -0500 Subject: [PATCH 23/26] Fix docstring spelling mistakes --- src/libcore/macros.rs | 7 +++---- src/libcore/pin.rs | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index a0c87f13e5d5a..c008b78e45092 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -350,9 +350,8 @@ macro_rules! try { /// assert_eq!(v, b"s = \"abc 123\""); /// ``` /// -/// Note: This macro can be used in `no_std` setups as well -/// In a `no_std` setup you are responsible for the -/// implementation details of the components. +/// Note: This macro can be used in `no_std` setups as well. +/// In a `no_std` setup you are responsible for the implementation details of the components. /// /// ```no_run /// # extern crate core; @@ -440,7 +439,7 @@ macro_rules! writeln { /// /// If the determination that the code is unreachable proves incorrect, the /// program immediately terminates with a [`panic!`]. The function [`unreachable_unchecked`], -/// which belongs to the [`std::hint`] module, informs the compilier to +/// which belongs to the [`std::hint`] module, informs the compiler to /// optimize the code out of the release version entirely. /// /// [`panic!`]: ../std/macro.panic.html diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index 68de82d294529..308dd9c79fa37 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -3,7 +3,7 @@ //! It is sometimes useful to have objects that are guaranteed to not move, //! in the sense that their placement in memory does not change, and can thus be relied upon. //! -//! A prime example of such a scenario would be building self-referencial structs, +//! A prime example of such a scenario would be building self-referential structs, //! since moving an object with pointers to itself will invalidate them, //! which could cause undefined behavior. //! @@ -39,7 +39,7 @@ //! use std::marker::Pinned; //! use std::ptr::NonNull; //! -//! // This is a self referencial struct since the slice field points to the data field. +//! // This is a self-referential struct since the slice field points to the data field. //! // We cannot inform the compiler about that with a normal reference, //! // since this pattern cannot be described with the usual borrowing rules. //! // Instead we use a raw pointer, though one which is known to not be null, From 9b4d68e53bd0eaa1ef3e402d0227f2d9ef1970dd Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sat, 10 Nov 2018 19:31:49 +0700 Subject: [PATCH 24/26] Fix documentation typos. --- src/libcore/alloc.rs | 4 ++-- src/libcore/future/future.rs | 2 +- src/libcore/lib.rs | 2 +- src/libcore/mem.rs | 8 ++++---- src/libcore/ptr.rs | 8 ++++---- src/libstd/ffi/c_str.rs | 2 +- src/libstd/lib.rs | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 113a85abecbef..9bd56442695f6 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -506,7 +506,7 @@ pub unsafe trait GlobalAlloc { ptr } - /// Shink or grow a block of memory to the given `new_size`. + /// Shrink or grow a block of memory to the given `new_size`. /// The block is described by the given `ptr` pointer and `layout`. /// /// If this returns a non-null pointer, then ownership of the memory block @@ -757,7 +757,7 @@ pub unsafe trait Alloc { // realloc. alloc_excess, realloc_excess /// Returns a pointer suitable for holding data described by - /// a new layout with `layout`’s alginment and a size given + /// a new layout with `layout`’s alignment and a size given /// by `new_size`. To /// accomplish this, this may extend or shrink the allocation /// referenced by `ptr` to fit the new layout. diff --git a/src/libcore/future/future.rs b/src/libcore/future/future.rs index 9176e0d32cbf2..0c870f9e404a2 100644 --- a/src/libcore/future/future.rs +++ b/src/libcore/future/future.rs @@ -17,7 +17,7 @@ use ops; use pin::Pin; use task::{Poll, LocalWaker}; -/// A future represents an asychronous computation. +/// A future represents an asynchronous computation. /// /// A future is a value that may not have finished computing yet. This kind of /// "asynchronous value" makes it possible for a thread to continue doing useful diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 1bbc7892c6bef..c69d4441121ce 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -228,7 +228,7 @@ mod nonzero; mod tuple; mod unit; -// Pull in the the `coresimd` crate directly into libcore. This is where all the +// Pull in the `coresimd` crate directly into libcore. This is where all the // architecture-specific (and vendor-specific) intrinsics are defined. AKA // things like SIMD and such. Note that the actual source for all this lies in a // different repository, rust-lang-nursery/stdsimd. That's why the setup here is diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 1d0b194487e68..8c4ff02aa140f 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -202,7 +202,7 @@ pub fn forget(t: T) { /// /// ## Size of Enums /// -/// Enums that carry no data other than the descriminant have the same size as C enums +/// Enums that carry no data other than the discriminant have the same size as C enums /// on the platform they are compiled for. /// /// ## Size of Unions @@ -1081,7 +1081,7 @@ impl MaybeUninit { /// /// # Unsafety /// - /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized + /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized /// state, otherwise this will immediately cause undefined behavior. #[unstable(feature = "maybe_uninit", issue = "53491")] pub unsafe fn into_inner(self) -> T { @@ -1092,7 +1092,7 @@ impl MaybeUninit { /// /// # Unsafety /// - /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized + /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized /// state, otherwise this will immediately cause undefined behavior. #[unstable(feature = "maybe_uninit", issue = "53491")] pub unsafe fn get_ref(&self) -> &T { @@ -1103,7 +1103,7 @@ impl MaybeUninit { /// /// # Unsafety /// - /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized + /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized /// state, otherwise this will immediately cause undefined behavior. #[unstable(feature = "maybe_uninit", issue = "53491")] pub unsafe fn get_mut(&mut self) -> &mut T { diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 62ccf6c865cd9..827e297c84d1f 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -120,7 +120,7 @@ pub use intrinsics::write_bytes; /// /// Additionally, if `T` is not [`Copy`], using the pointed-to value after /// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop = -/// foo` counts as a use because it will cause the the value to be dropped +/// foo` counts as a use because it will cause the value to be dropped /// again. [`write`] can be used to overwrite data without causing it to be /// dropped. /// @@ -371,7 +371,7 @@ pub(crate) unsafe fn swap_nonoverlapping_one(x: *mut T, y: *mut T) { #[inline] unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) { // The approach here is to utilize simd to swap x & y efficiently. Testing reveals - // that swapping either 32 bytes or 64 bytes at a time is most efficient for intel + // that swapping either 32 bytes or 64 bytes at a time is most efficient for Intel // Haswell E processors. LLVM is more able to optimize if we give a struct a // #[repr(simd)], even if we don't actually use this struct directly. // @@ -1005,7 +1005,7 @@ impl *const T { /// # Null-unchecked version /// /// If you are sure the pointer can never be null and are looking for some kind of - /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>, know that you can + /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can /// dereference the pointer directly. /// /// ``` @@ -1625,7 +1625,7 @@ impl *mut T { /// # Null-unchecked version /// /// If you are sure the pointer can never be null and are looking for some kind of - /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>, know that you can + /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can /// dereference the pointer directly. /// /// ``` diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index dfec13cd2ec00..87ffe0f15e454 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -1178,7 +1178,7 @@ impl CStr { /// /// If the contents of the `CStr` are valid UTF-8 data, this /// function will return a [`Cow`]`::`[`Borrowed`]`(`[`&str`]`)` - /// with the the corresponding [`&str`] slice. Otherwise, it will + /// with the corresponding [`&str`] slice. Otherwise, it will /// replace any invalid UTF-8 sequences with /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a /// [`Cow`]`::`[`Owned`]`(`[`String`]`)` with the result. diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index f27beb0b46caf..0829593505d69 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -495,7 +495,7 @@ mod memchr; // compiler pub mod rt; -// Pull in the the `stdsimd` crate directly into libstd. This is the same as +// Pull in the `stdsimd` crate directly into libstd. This is the same as // libcore's arch/simd modules where the source of truth here is in a different // repository, but we pull things in here manually to get it into libstd. // From 48aa60254863a6c495340e2f1b753f7b9a45a9d0 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 10 Nov 2018 14:09:49 +0100 Subject: [PATCH 25/26] Set BINARYEN_TRAP_MODE=clamp This avoids trapping in the -Zsaturating-float-casts implementation. --- src/librustc_target/spec/wasm32_unknown_emscripten.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/librustc_target/spec/wasm32_unknown_emscripten.rs b/src/librustc_target/spec/wasm32_unknown_emscripten.rs index b4c09f86b8a97..2c80f3b4b3b02 100644 --- a/src/librustc_target/spec/wasm32_unknown_emscripten.rs +++ b/src/librustc_target/spec/wasm32_unknown_emscripten.rs @@ -11,12 +11,18 @@ use super::{LinkArgs, LinkerFlavor, Target, TargetOptions}; pub fn target() -> Result { + // FIXME(nikic) BINARYEN_TRAP_MODE=clamp is needed to avoid trapping in our + // -Zsaturating-float-casts implementation. This can be dropped if/when + // we have native fpto[su]i.sat intrinsics, or the implementation otherwise + // stops relying on non-trapping fpto[su]i. let mut post_link_args = LinkArgs::new(); post_link_args.insert(LinkerFlavor::Em, vec!["-s".to_string(), "BINARYEN=1".to_string(), "-s".to_string(), - "ERROR_ON_UNDEFINED_SYMBOLS=1".to_string()]); + "ERROR_ON_UNDEFINED_SYMBOLS=1".to_string(), + "-s".to_string(), + "BINARYEN_TRAP_MODE='clamp'".to_string()]); let opts = TargetOptions { dynamic_linking: false, From 2f8ce732e17b2dc4f5a3f1568ada9430a1a899db Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Sat, 10 Nov 2018 16:05:29 -0600 Subject: [PATCH 26/26] move all static-file include!s into a single module --- src/librustdoc/config.rs | 5 +- src/librustdoc/html/render.rs | 50 ++++++------- src/librustdoc/html/static_files.rs | 111 ++++++++++++++++++++++++++++ src/librustdoc/lib.rs | 1 + 4 files changed, 140 insertions(+), 27 deletions(-) create mode 100644 src/librustdoc/html/static_files.rs diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 903aafed64116..7dafe67485a6e 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -29,6 +29,7 @@ use core::new_handler; use externalfiles::ExternalHtml; use html; use html::markdown::IdMap; +use html::static_files; use opts; use passes::{self, DefaultPassOption}; use theme; @@ -261,7 +262,7 @@ impl Options { let to_check = matches.opt_strs("theme-checker"); if !to_check.is_empty() { - let paths = theme::load_css_paths(include_bytes!("html/static/themes/light.css")); + let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes()); let mut errors = 0; println!("rustdoc: [theme-checker] Starting tests!"); @@ -338,7 +339,7 @@ impl Options { let mut themes = Vec::new(); if matches.opt_present("themes") { - let paths = theme::load_css_paths(include_bytes!("html/static/themes/light.css")); + let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes()); for (theme_file, theme_s) in matches.opt_strs("themes") .iter() diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index efd71ad0763e0..f560350d5105d 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -76,7 +76,7 @@ use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace}; use html::format::fmt_impl_for_trait_page; use html::item_type::ItemType; use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap}; -use html::{highlight, layout}; +use html::{highlight, layout, static_files}; use minifier; @@ -767,10 +767,10 @@ fn write_shared( // overwrite them anyway to make sure that they're fresh and up-to-date. write_minify(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)), - include_str!("static/rustdoc.css"), + static_files::RUSTDOC_CSS, options.enable_minification)?; write_minify(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)), - include_str!("static/settings.css"), + static_files::SETTINGS_CSS, options.enable_minification)?; // To avoid "light.css" to be overwritten, we'll first run over the received themes and only @@ -790,15 +790,15 @@ fn write_shared( } write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)), - include_bytes!("static/brush.svg"))?; + static_files::BRUSH_SVG)?; write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)), - include_bytes!("static/wheel.svg"))?; + static_files::WHEEL_SVG)?; write_minify(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)), - include_str!("static/themes/light.css"), + static_files::themes::LIGHT, options.enable_minification)?; themes.insert("light".to_owned()); write_minify(cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)), - include_str!("static/themes/dark.css"), + static_files::themes::DARK, options.enable_minification)?; themes.insert("dark".to_owned()); @@ -854,16 +854,16 @@ themePicker.onblur = handleThemeButtonsBlur; )?; write_minify(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)), - include_str!("static/main.js"), + static_files::MAIN_JS, options.enable_minification)?; write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)), - include_str!("static/settings.js"), + static_files::SETTINGS_JS, options.enable_minification)?; { let mut data = format!("var resourcesSuffix = \"{}\";\n", cx.shared.resource_suffix); - data.push_str(include_str!("static/storage.js")); + data.push_str(static_files::STORAGE_JS); write_minify(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)), &data, options.enable_minification)?; @@ -882,36 +882,36 @@ themePicker.onblur = handleThemeButtonsBlur; } } write_minify(cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)), - include_str!("static/normalize.css"), + static_files::NORMALIZE_CSS, options.enable_minification)?; write(cx.dst.join("FiraSans-Regular.woff"), - include_bytes!("static/FiraSans-Regular.woff"))?; + static_files::fira_sans::REGULAR)?; write(cx.dst.join("FiraSans-Medium.woff"), - include_bytes!("static/FiraSans-Medium.woff"))?; + static_files::fira_sans::MEDIUM)?; write(cx.dst.join("FiraSans-LICENSE.txt"), - include_bytes!("static/FiraSans-LICENSE.txt"))?; + static_files::fira_sans::LICENSE)?; write(cx.dst.join("Heuristica-Italic.woff"), - include_bytes!("static/Heuristica-Italic.woff"))?; + static_files::heuristica::ITALIC)?; write(cx.dst.join("Heuristica-LICENSE.txt"), - include_bytes!("static/Heuristica-LICENSE.txt"))?; + static_files::heuristica::LICENSE)?; write(cx.dst.join("SourceSerifPro-Regular.woff"), - include_bytes!("static/SourceSerifPro-Regular.woff"))?; + static_files::source_serif_pro::REGULAR)?; write(cx.dst.join("SourceSerifPro-Bold.woff"), - include_bytes!("static/SourceSerifPro-Bold.woff"))?; + static_files::source_serif_pro::BOLD)?; write(cx.dst.join("SourceSerifPro-LICENSE.txt"), - include_bytes!("static/SourceSerifPro-LICENSE.txt"))?; + static_files::source_serif_pro::LICENSE)?; write(cx.dst.join("SourceCodePro-Regular.woff"), - include_bytes!("static/SourceCodePro-Regular.woff"))?; + static_files::source_code_pro::REGULAR)?; write(cx.dst.join("SourceCodePro-Semibold.woff"), - include_bytes!("static/SourceCodePro-Semibold.woff"))?; + static_files::source_code_pro::SEMIBOLD)?; write(cx.dst.join("SourceCodePro-LICENSE.txt"), - include_bytes!("static/SourceCodePro-LICENSE.txt"))?; + static_files::source_code_pro::LICENSE)?; write(cx.dst.join("LICENSE-MIT.txt"), - include_bytes!("static/LICENSE-MIT.txt"))?; + static_files::LICENSE_MIT)?; write(cx.dst.join("LICENSE-APACHE.txt"), - include_bytes!("static/LICENSE-APACHE.txt"))?; + static_files::LICENSE_APACHE)?; write(cx.dst.join("COPYRIGHT.txt"), - include_bytes!("static/COPYRIGHT.txt"))?; + static_files::COPYRIGHT)?; fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec, Vec)> { let mut ret = Vec::new(); diff --git a/src/librustdoc/html/static_files.rs b/src/librustdoc/html/static_files.rs new file mode 100644 index 0000000000000..3baa082bd0e69 --- /dev/null +++ b/src/librustdoc/html/static_files.rs @@ -0,0 +1,111 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Static files bundled with documentation output. +//! +//! All the static files are included here for centralized access in case anything other than the +//! HTML rendering code (say, the theme checker) needs to access one of these files. +//! +//! Note about types: CSS and JavaScript files are included as `&'static str` to allow for the +//! minifier to run on them. All other files are included as `&'static [u8]` so they can be +//! directly written to a `Write` handle. + +/// The file contents of the main `rustdoc.css` file, responsible for the core layout of the page. +pub static RUSTDOC_CSS: &'static str = include_str!("static/rustdoc.css"); + +/// The file contents of `settings.css`, responsible for the items on the settings page. +pub static SETTINGS_CSS: &'static str = include_str!("static/settings.css"); + +/// The file contents of `normalize.css`, included to even out standard elements between browser +/// implementations. +pub static NORMALIZE_CSS: &'static str = include_str!("static/normalize.css"); + +/// The file contents of `main.js`, which contains the core JavaScript used on documentation pages, +/// including search behavior and docblock folding, among others. +pub static MAIN_JS: &'static str = include_str!("static/main.js"); + +/// The file contents of `settings.js`, which contains the JavaScript used to handle the settings +/// page. +pub static SETTINGS_JS: &'static str = include_str!("static/settings.js"); + +/// The file contents of `storage.js`, which contains functionality related to browser Local +/// Storage, used to store documentation settings. +pub static STORAGE_JS: &'static str = include_str!("static/storage.js"); + +/// The file contents of `brush.svg`, the icon used for the theme-switch button. +pub static BRUSH_SVG: &'static [u8] = include_bytes!("static/brush.svg"); + +/// The file contents of `wheel.svg`, the icon used for the settings button. +pub static WHEEL_SVG: &'static [u8] = include_bytes!("static/wheel.svg"); + +/// The contents of `COPYRIGHT.txt`, the license listing for files distributed with documentation +/// output. +pub static COPYRIGHT: &'static [u8] = include_bytes!("static/COPYRIGHT.txt"); + +/// The contents of `LICENSE-APACHE.txt`, the text of the Apache License, version 2.0. +pub static LICENSE_APACHE: &'static [u8] = include_bytes!("static/LICENSE-APACHE.txt"); + +/// The contents of `LICENSE-MIT.txt`, the text of the MIT License. +pub static LICENSE_MIT: &'static [u8] = include_bytes!("static/LICENSE-MIT.txt"); + +/// The built-in themes given to every documentation site. +pub mod themes { + /// The "light" theme, selected by default when no setting is available. Used as the basis for + /// the `--theme-checker` functionality. + pub static LIGHT: &'static str = include_str!("static/themes/light.css"); + + /// The "dark" theme. + pub static DARK: &'static str = include_str!("static/themes/dark.css"); +} + +/// Files related to the Fira Sans font. +pub mod fira_sans { + /// The file `FiraSans-Regular.woff`, the Regular variant of the Fira Sans font. + pub static REGULAR: &'static [u8] = include_bytes!("static/FiraSans-Regular.woff"); + + /// The file `FiraSans-Medium.woff`, the Medium variant of the Fira Sans font. + pub static MEDIUM: &'static [u8] = include_bytes!("static/FiraSans-Medium.woff"); + + /// The file `FiraSans-LICENSE.txt`, the license text for the Fira Sans font. + pub static LICENSE: &'static [u8] = include_bytes!("static/FiraSans-LICENSE.txt"); +} + +/// Files related to the Heuristica font. +pub mod heuristica { + /// The file `Heuristica-Italic.woff`, the Italic variant of the Heuristica font. + pub static ITALIC: &'static [u8] = include_bytes!("static/Heuristica-Italic.woff"); + + /// The file `Heuristica-LICENSE.txt`, the license text for the Heuristica font. + pub static LICENSE: &'static [u8] = include_bytes!("static/Heuristica-LICENSE.txt"); +} + +/// Files related to the Source Serif Pro font. +pub mod source_serif_pro { + /// The file `SourceSerifPro-Regular.woff`, the Regular variant of the Source Serif Pro font. + pub static REGULAR: &'static [u8] = include_bytes!("static/SourceSerifPro-Regular.woff"); + + /// The file `SourceSerifPro-Bold.woff`, the Bold variant of the Source Serif Pro font. + pub static BOLD: &'static [u8] = include_bytes!("static/SourceSerifPro-Bold.woff"); + + /// The file `SourceSerifPro-LICENSE.txt`, the license text for the Source Serif Pro font. + pub static LICENSE: &'static [u8] = include_bytes!("static/SourceSerifPro-LICENSE.txt"); +} + +/// Files related to the Source Code Pro font. +pub mod source_code_pro { + /// The file `SourceCodePro-Regular.woff`, the Regular variant of the Source Code Pro font. + pub static REGULAR: &'static [u8] = include_bytes!("static/SourceCodePro-Regular.woff"); + + /// The file `SourceCodePro-Semibold.woff`, the Semibold variant of the Source Code Pro font. + pub static SEMIBOLD: &'static [u8] = include_bytes!("static/SourceCodePro-Semibold.woff"); + + /// The file `SourceCodePro-LICENSE.txt`, the license text of the Source Code Pro font. + pub static LICENSE: &'static [u8] = include_bytes!("static/SourceCodePro-LICENSE.txt"); +} diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index f0f36f0355ed6..4b043b26d8650 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -78,6 +78,7 @@ pub mod html { crate mod layout; pub mod markdown; crate mod render; + crate mod static_files; crate mod toc; } mod markdown;