about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-03-16 11:13:13 +0000
committerbors <bors@rust-lang.org>2018-03-16 11:13:13 +0000
commitcc34ca1c9787fde84116637a0cee92fc5e375e3d (patch)
tree2964be90a43e6248d100e1bafb438d52e935167f /src
parent5f3996c3ec4824b92b2af251ac09406f9573e1ff (diff)
parent5218c2d5efbb2907b279e627782e96896308d106 (diff)
Auto merge of #48818 - michaelwoerister:issue-47309, r=eddyb
Properly handle collecting default impls of methods with lifetime parameters.

r? @eddyb

Fixes #47309.
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/monomorphize/collector.rs28
-rw-r--r--src/test/compile-fail/issue-47309.rs31
2 files changed, 48 insertions, 11 deletions
diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs
index 7fd2ea265de..446ef6bd328 100644
--- a/src/librustc_mir/monomorphize/collector.rs
+++ b/src/librustc_mir/monomorphize/collector.rs
@@ -341,6 +341,8 @@ fn collect_roots<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
         };
 
         tcx.hir.krate().visit_all_item_likes(&mut visitor);
+
+        visitor.push_extra_entry_roots();
     }
 
     // We can only translate items that are instantiable - items all of
@@ -998,8 +1000,6 @@ impl<'b, 'a, 'v> RootCollector<'b, 'a, 'v> {
 
             let instance = Instance::mono(self.tcx, def_id);
             self.output.push(create_fn_mono_item(instance));
-
-            self.push_extra_entry_roots(def_id);
         }
     }
 
@@ -1008,20 +1008,22 @@ impl<'b, 'a, 'v> RootCollector<'b, 'a, 'v> {
     /// monomorphized copy of the start lang item based on
     /// the return type of `main`. This is not needed when
     /// the user writes their own `start` manually.
-    fn push_extra_entry_roots(&mut self, def_id: DefId) {
-        if self.entry_fn != Some(def_id) {
-            return;
-        }
-
+    fn push_extra_entry_roots(&mut self) {
         if self.tcx.sess.entry_type.get() != Some(config::EntryMain) {
-            return;
+            return
         }
 
+        let main_def_id = if let Some(def_id) = self.entry_fn {
+            def_id
+        } else {
+            return
+        };
+
         let start_def_id = match self.tcx.lang_items().require(StartFnLangItem) {
             Ok(s) => s,
             Err(err) => self.tcx.sess.fatal(&err),
         };
-        let main_ret_ty = self.tcx.fn_sig(def_id).output();
+        let main_ret_ty = self.tcx.fn_sig(main_def_id).output();
 
         // Given that `main()` has no arguments,
         // then its return type cannot have
@@ -1066,7 +1068,6 @@ fn create_mono_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                    def_id_to_string(tcx, impl_def_id));
 
             if let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) {
-                let callee_substs = tcx.erase_regions(&trait_ref.substs);
                 let overridden_methods: FxHashSet<_> =
                     impl_item_refs.iter()
                                   .map(|iiref| iiref.name)
@@ -1080,10 +1081,15 @@ fn create_mono_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                         continue;
                     }
 
+                    let substs = Substs::for_item(tcx,
+                                                  method.def_id,
+                                                  |_, _| tcx.types.re_erased,
+                                                  |def, _| trait_ref.substs.type_for_def(def));
+
                     let instance = ty::Instance::resolve(tcx,
                                                          ty::ParamEnv::reveal_all(),
                                                          method.def_id,
-                                                         callee_substs).unwrap();
+                                                         substs).unwrap();
 
                     let mono_item = create_fn_mono_item(instance);
                     if mono_item.is_instantiable(tcx)
diff --git a/src/test/compile-fail/issue-47309.rs b/src/test/compile-fail/issue-47309.rs
new file mode 100644
index 00000000000..7141bd62dc0
--- /dev/null
+++ b/src/test/compile-fail/issue-47309.rs
@@ -0,0 +1,31 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Make sure that the mono-item collector does not crash when trying to
+// instantiate a default impl of a method with lifetime parameters.
+// See https://github.com/rust-lang/rust/issues/47309
+
+// compile-flags:-Clink-dead-code
+// must-compile-successfully
+
+#![crate_type="rlib"]
+
+pub trait EnvFuture {
+    type Item;
+
+    fn boxed_result<'a>(self) where Self: Sized, Self::Item: 'a, {
+    }
+}
+
+struct Foo;
+
+impl<'a> EnvFuture for &'a Foo {
+    type Item = ();
+}