diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index 4600cdbc692e7..92bbb745bb214 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -476,6 +476,7 @@ define_dep_nodes!( <'tcx> [] TypeOfItem(DefId), [] GenericsOfItem(DefId), [] PredicatesOfItem(DefId), + [] InferredOutlivesOf(DefId), [] SuperPredicatesOfItem(DefId), [] TraitDefOfItem(DefId), [] AdtDefOfItem(DefId), diff --git a/src/librustc/ty/maps/mod.rs b/src/librustc/ty/maps/mod.rs index 58405c261ad72..f54391ebb07eb 100644 --- a/src/librustc/ty/maps/mod.rs +++ b/src/librustc/ty/maps/mod.rs @@ -121,6 +121,9 @@ define_maps! { <'tcx> /// (inferred) variance. [] fn variances_of: ItemVariances(DefId) -> Rc>, + /// Maps from def-id of a type to its (inferred) outlives. + [] fn inferred_outlives_of: InferredOutlivesOf(DefId) -> Vec>, + /// Maps from an impl/trait def-id to a list of the def-ids of its items [] fn associated_item_def_ids: AssociatedItemDefIds(DefId) -> Rc>, diff --git a/src/librustc/ty/maps/plumbing.rs b/src/librustc/ty/maps/plumbing.rs index d6eaf6d1bc48f..4e301342ee079 100644 --- a/src/librustc/ty/maps/plumbing.rs +++ b/src/librustc/ty/maps/plumbing.rs @@ -736,6 +736,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>, DepKind::TypeOfItem => { force!(type_of, def_id!()); } DepKind::GenericsOfItem => { force!(generics_of, def_id!()); } DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); } + DepKind::InferredOutlivesOf => { force!(inferred_outlives_of, def_id!()); } DepKind::SuperPredicatesOfItem => { force!(super_predicates_of, def_id!()); } DepKind::TraitDefOfItem => { force!(trait_def, def_id!()); } DepKind::AdtDefOfItem => { force!(adt_def, def_id!()); } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 25a37a2c48cdb..a5b3f8cb806be 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1332,7 +1332,11 @@ fn early_bound_lifetimes_from_generics<'a, 'tcx>( fn predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::GenericPredicates<'tcx> { - explicit_predicates_of(tcx, def_id) + let explicit = explicit_predicates_of(tcx, def_id); + ty::GenericPredicates { + parent: explicit.parent, + predicates: [&explicit.predicates[..], &tcx.inferred_outlives_of(def_id)[..]].concat() + } } fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 8df973555744b..8f91d07b53fb2 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4676,6 +4676,7 @@ register_diagnostics! { E0588, // packed struct cannot transitively contain a `[repr(align)]` struct E0592, // duplicate definitions with name `{}` // E0613, // Removed (merged with E0609) + E0640, // infer outlives E0627, // yield statement outside of generator literal E0632, // cannot provide explicit type parameters when `impl Trait` is used in // argument position. diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 1c047ef98d831..49ba0499f7868 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -50,6 +50,8 @@ independently: - variance: variance inference +- outlives: outlives inference + - check: walks over function bodies and type checks them, inferring types for local variables, type parameters, etc as necessary. @@ -122,6 +124,7 @@ mod collect; mod constrained_type_params; mod impl_wf_check; mod coherence; +mod outlives; mod variance; mod namespace; @@ -286,6 +289,7 @@ pub fn provide(providers: &mut Providers) { coherence::provide(providers); check::provide(providers); variance::provide(providers); + outlives::provide(providers); } pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) @@ -301,6 +305,11 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) })?; + tcx.sess.track_errors(|| { + time(time_passes, "outlives testing", || + outlives::test::test_inferred_outlives(tcx)); + })?; + tcx.sess.track_errors(|| { time(time_passes, "impl wf inference", || impl_wf_check::impl_wf_check(tcx)); diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs new file mode 100644 index 0000000000000..1127028cbc8c7 --- /dev/null +++ b/src/librustc_typeck/outlives/mod.rs @@ -0,0 +1,29 @@ +// Copyright 2013 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. + +use rustc::hir::def_id::DefId; +use rustc::ty::{self, TyCtxt}; +use rustc::ty::maps::Providers; + +/// Code to write unit test for outlives. +pub mod test; + +pub fn provide(providers: &mut Providers) { + *providers = Providers { + inferred_outlives_of, + ..*providers + }; +} + +//todo +fn inferred_outlives_of<'a, 'tcx>(_tcx: TyCtxt<'a, 'tcx, 'tcx>, _def_id: DefId) + -> Vec> { + Vec::new() +} diff --git a/src/librustc_typeck/outlives/test.rs b/src/librustc_typeck/outlives/test.rs new file mode 100644 index 0000000000000..196e660549463 --- /dev/null +++ b/src/librustc_typeck/outlives/test.rs @@ -0,0 +1,41 @@ +// Copyright 2013 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. + +use rustc::hir; +use rustc::hir::itemlikevisit::ItemLikeVisitor; +use rustc::ty::TyCtxt; + +pub fn test_inferred_outlives<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) { + tcx.hir.krate().visit_all_item_likes(&mut OutlivesTest { tcx }); +} + +struct OutlivesTest<'a, 'tcx: 'a> { + tcx: TyCtxt<'a, 'tcx, 'tcx> +} + +impl<'a, 'tcx> ItemLikeVisitor<'tcx> for OutlivesTest<'a, 'tcx> { + fn visit_item(&mut self, item: &'tcx hir::Item) { + let item_def_id = self.tcx.hir.local_def_id(item.id); + + // For unit testing: check for a special "rustc_outlives" + // attribute and report an error with various results if found. + if self.tcx.has_attr(item_def_id, "rustc_outlives") { + let inferred_outlives_of = self.tcx.inferred_outlives_of(item_def_id); + span_err!(self.tcx.sess, + item.span, + E0640, + "{:?}", + inferred_outlives_of); + } + } + + fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) { } + fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { } +} diff --git a/src/test/compile-fail/outlives-associated-types.rs b/src/test/compile-fail/outlives-associated-types.rs new file mode 100644 index 0000000000000..778394c9fc8ad --- /dev/null +++ b/src/test/compile-fail/outlives-associated-types.rs @@ -0,0 +1,23 @@ +// 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. + +// Test that the outlives computation runs for now... + +#![feature(rustc_attrs)] + +//todo add all the test cases +// https://github.com/rust-lang/rfcs/blob/master/text/2093-infer-outlives.md#example-1-a-reference + +#[rustc_outlives] +struct Direct<'a, T> { //~ ERROR 19:1: 21:2: [] [E0640] + field: &'a T +} + +fn main() { }