Skip to content

Commit c28cbfb

Browse files
committed
Auto merge of #42797 - arielb1:ex-falso-ice, r=nikomatsakis
avoid translating roots with predicates that do not hold Finally I got around to doing this. Fixes #37725. r? @nikomatsakis
2 parents 69c65d2 + a6ca302 commit c28cbfb

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

Diff for: src/librustc/traits/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ pub fn normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
608608
debug!("normalize_and_test_predicates(predicates={:?})",
609609
predicates);
610610

611-
tcx.infer_ctxt().enter(|infcx| {
611+
let result = tcx.infer_ctxt().enter(|infcx| {
612612
let param_env = ty::ParamEnv::empty(Reveal::All);
613613
let mut selcx = SelectionContext::new(&infcx);
614614
let mut fulfill_cx = FulfillmentContext::new();
@@ -624,7 +624,10 @@ pub fn normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
624624
}
625625

626626
fulfill_cx.select_all_or_error(&infcx).is_ok()
627-
})
627+
});
628+
debug!("normalize_and_test_predicates(predicates={:?}) = {:?}",
629+
predicates, result);
630+
result
628631
}
629632

630633
/// Given a trait `trait_ref`, iterates the vtable entries

Diff for: src/librustc_trans/collector.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ use rustc::hir::map as hir_map;
195195
use rustc::hir::def_id::DefId;
196196
use rustc::middle::lang_items::{ExchangeMallocFnLangItem};
197197
use rustc::traits;
198-
use rustc::ty::subst::{Substs, Subst};
198+
use rustc::ty::subst::Substs;
199199
use rustc::ty::{self, TypeFoldable, TyCtxt};
200200
use rustc::ty::adjustment::CustomCoerceUnsized;
201201
use rustc::mir::{self, Location};
@@ -304,6 +304,11 @@ fn collect_roots<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
304304
scx.tcx().hir.krate().visit_all_item_likes(&mut visitor);
305305
}
306306

307+
// We can only translate items that are instantiable - items all of
308+
// whose predicates hold. Luckily, items that aren't instantiable
309+
// can't actually be used, so we can just skip translating them.
310+
roots.retain(|root| root.is_instantiable(scx.tcx()));
311+
307312
roots
308313
}
309314

@@ -937,14 +942,9 @@ fn create_trans_items_for_default_impls<'a, 'tcx>(scx: &SharedCrateContext<'a, '
937942
let instance =
938943
monomorphize::resolve(scx, method.def_id, callee_substs);
939944

940-
let predicates = tcx.predicates_of(instance.def_id()).predicates
941-
.subst(tcx, instance.substs);
942-
if !traits::normalize_and_test_predicates(tcx, predicates) {
943-
continue;
944-
}
945-
946-
if should_trans_locally(tcx, &instance) {
947-
output.push(create_fn_trans_item(instance));
945+
let trans_item = create_fn_trans_item(instance);
946+
if trans_item.is_instantiable(tcx) && should_trans_locally(tcx, &instance) {
947+
output.push(trans_item);
948948
}
949949
}
950950
}

Diff for: src/librustc_trans/trans_item.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ use llvm;
2525
use monomorphize::Instance;
2626
use rustc::hir;
2727
use rustc::hir::def_id::DefId;
28+
use rustc::traits;
2829
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
29-
use rustc::ty::subst::Substs;
30+
use rustc::ty::subst::{Subst, Substs};
3031
use syntax::ast::{self, NodeId};
3132
use syntax::attr;
3233
use syntax_pos::Span;
@@ -250,6 +251,44 @@ impl<'a, 'tcx> TransItem<'tcx> {
250251
}
251252
}
252253

254+
/// Returns whether this instance is instantiable - whether it has no unsatisfied
255+
/// predicates.
256+
///
257+
/// In order to translate an item, all of its predicates must hold, because
258+
/// otherwise the item does not make sense. Type-checking ensures that
259+
/// the predicates of every item that is *used by* a valid item *do*
260+
/// hold, so we can rely on that.
261+
///
262+
/// However, we translate collector roots (reachable items) and functions
263+
/// in vtables when they are seen, even if they are not used, and so they
264+
/// might not be instantiable. For example, a programmer can define this
265+
/// public function:
266+
///
267+
/// pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
268+
/// <&mut () as Clone>::clone(&s);
269+
/// }
270+
///
271+
/// That function can't be translated, because the method `<&mut () as Clone>::clone`
272+
/// does not exist. Luckily for us, that function can't ever be used,
273+
/// because that would require for `&'a mut (): Clone` to hold, so we
274+
/// can just not emit any code, or even a linker reference for it.
275+
///
276+
/// Similarly, if a vtable method has such a signature, and therefore can't
277+
/// be used, we can just not emit it and have a placeholder (a null pointer,
278+
/// which will never be accessed) in its place.
279+
pub fn is_instantiable(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> bool {
280+
debug!("is_instantiable({:?})", self);
281+
let (def_id, substs) = match *self {
282+
TransItem::Fn(ref instance) => (instance.def_id(), instance.substs),
283+
TransItem::Static(node_id) => (tcx.hir.local_def_id(node_id), Substs::empty()),
284+
// global asm never has predicates
285+
TransItem::GlobalAsm(..) => return true
286+
};
287+
288+
let predicates = tcx.predicates_of(def_id).predicates.subst(tcx, substs);
289+
traits::normalize_and_test_predicates(tcx, predicates)
290+
}
291+
253292
pub fn to_string(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> String {
254293
let hir_map = &tcx.hir;
255294

Diff for: src/test/run-pass/issue-37725.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
12+
s.clone();
13+
}
14+
fn main() {}

0 commit comments

Comments
 (0)