Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ pub fn normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
debug!("normalize_and_test_predicates(predicates={:?})",
predicates);

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

fulfill_cx.select_all_or_error(&infcx).is_ok()
})
});
debug!("normalize_and_test_predicates(predicates={:?}) = {:?}",
predicates, result);
result
}

/// Given a trait `trait_ref`, iterates the vtable entries
Expand Down
14 changes: 5 additions & 9 deletions src/librustc_trans/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ use rustc::hir::map as hir_map;
use rustc::hir::def_id::DefId;
use rustc::middle::lang_items::{ExchangeMallocFnLangItem};
use rustc::traits;
use rustc::ty::subst::{Substs, Subst};
use rustc::ty::subst::Substs;
use rustc::ty::{self, TypeFoldable, TyCtxt};
use rustc::ty::adjustment::CustomCoerceUnsized;
use rustc::mir::{self, Location};
Expand Down Expand Up @@ -304,6 +304,7 @@ fn collect_roots<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
scx.tcx().hir.krate().visit_all_item_likes(&mut visitor);
}

roots.retain(|root| root.is_instantiable(scx.tcx()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This seems perfectly reasonable, but perhaps not optimal? (Also, why do we check below for whether something is instantiable, but not other places?)

Put another way, it might be better to have some helper routine for pushing into self.output that checks for "is instantiable" at that point -- perhaps this would also avoid enumerating the things reachable from a non-instantiable thing?

However, I see no obvious reason that this code is wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere except for roots and vtables, instantiability is guaranteed by the program type-checking, so we want to produce a clean ICE if the item is not instantiable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't the roots just the content of the things pushed to self.output?

roots
}

Expand Down Expand Up @@ -937,14 +938,9 @@ fn create_trans_items_for_default_impls<'a, 'tcx>(scx: &SharedCrateContext<'a, '
let instance =
monomorphize::resolve(scx, method.def_id, callee_substs);

let predicates = tcx.predicates_of(instance.def_id()).predicates
.subst(tcx, instance.substs);
if !traits::normalize_and_test_predicates(tcx, predicates) {
continue;
}

if should_trans_locally(tcx, &instance) {
output.push(create_fn_trans_item(instance));
let trans_item = create_fn_trans_item(instance);
if trans_item.is_instantiable(tcx) && should_trans_locally(tcx, &instance) {
output.push(trans_item);
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/librustc_trans/trans_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use llvm;
use monomorphize::Instance;
use rustc::hir;
use rustc::hir::def_id::DefId;
use rustc::traits;
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::ty::subst::Substs;
use rustc::ty::subst::{Subst, Substs};
use syntax::ast::{self, NodeId};
use syntax::attr;
use syntax_pos::Span;
Expand Down Expand Up @@ -250,6 +251,21 @@ impl<'a, 'tcx> TransItem<'tcx> {
}
}

/// Returns whether this instance is instantiable - whether it has no unsatisfied
/// predicates.
pub fn is_instantiable(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> bool {
debug!("is_instantiable({:?})", self);
let (def_id, substs) = match *self {
TransItem::Fn(ref instance) => (instance.def_id(), instance.substs),
TransItem::Static(node_id) => (tcx.hir.local_def_id(node_id), Substs::empty()),
// global asm never has predicates
TransItem::GlobalAsm(..) => return true
};

let predicates = tcx.predicates_of(def_id).predicates.subst(tcx, substs);
traits::normalize_and_test_predicates(tcx, predicates)
}

pub fn to_string(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> String {
let hir_map = &tcx.hir;

Expand Down
14 changes: 14 additions & 0 deletions src/test/run-pass/issue-37725.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2016 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.

pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
s.clone();
}
fn main() {}