Skip to content

Commit

Permalink
normalize types every time HR regions are erased
Browse files Browse the repository at this point in the history
Associated type normalization is inhibited by higher-ranked regions.
Therefore, every time we erase them, we must re-normalize.

I was meaning to introduce this change some time ago, but we used
to erase regions in generic context, which broke this terribly (because
you can't always normalize in a generic context). That seems to be gone
now.

Ensure this by having a `erase_late_bound_regions_and_normalize`
function.

Fixes #37109 (the missing call was in mir::block).
  • Loading branch information
arielb1 committed Oct 13, 2016
1 parent 68ca911 commit ee338c3
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 36 deletions.
15 changes: 14 additions & 1 deletion src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ impl_trans_normalize!('gcx,
ty::FnSig<'gcx>,
&'gcx ty::BareFnTy<'gcx>,
ty::ClosureSubsts<'gcx>,
ty::PolyTraitRef<'gcx>
ty::PolyTraitRef<'gcx>,
ty::ExistentialTraitRef<'gcx>
);

impl<'gcx> TransNormalize<'gcx> for LvalueTy<'gcx> {
Expand All @@ -603,6 +604,18 @@ impl<'gcx> TransNormalize<'gcx> for LvalueTy<'gcx> {

// NOTE: Callable from trans only!
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
/// Currently, higher-ranked type bounds inhibit normalization. Therefore,
/// each time we erase them in translation, we need to normalize
/// the contents.
pub fn erase_late_bound_regions_and_normalize<T>(self, value: &ty::Binder<T>)
-> T
where T: TransNormalize<'tcx>
{
assert!(!value.needs_subst());
let value = self.erase_late_bound_regions(value);
self.normalize_associated_type(&value)
}

pub fn normalize_associated_type<T>(self, value: &T) -> T
where T: TransNormalize<'tcx>
{
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,8 +1050,7 @@ pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance
let fn_ty = ccx.tcx().erase_regions(&fn_ty);
let fn_ty = monomorphize::apply_param_substs(ccx.shared(), instance.substs, &fn_ty);

let sig = ccx.tcx().erase_late_bound_regions(fn_ty.fn_sig());
let sig = ccx.tcx().normalize_associated_type(&sig);
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(fn_ty.fn_sig());
let abi = fn_ty.fn_abi();

let lldecl = match ccx.instances().borrow().get(&instance) {
Expand All @@ -1073,8 +1072,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let ctor_ty = ccx.tcx().lookup_item_type(def_id).ty;
let ctor_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &ctor_ty);

let sig = ccx.tcx().erase_late_bound_regions(&ctor_ty.fn_sig());
let sig = ccx.tcx().normalize_associated_type(&sig);
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&ctor_ty.fn_sig());
let fn_ty = FnType::new(ccx, Abi::Rust, &sig, &[]);

let (arena, fcx): (TypedArena<_>, FunctionContext);
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ impl<'tcx> Callee<'tcx> {
pub fn direct_fn_type<'a>(&self, ccx: &CrateContext<'a, 'tcx>,
extra_args: &[Ty<'tcx>]) -> FnType {
let abi = self.ty.fn_abi();
let sig = ccx.tcx().erase_late_bound_regions(self.ty.fn_sig());
let sig = ccx.tcx().normalize_associated_type(&sig);
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(self.ty.fn_sig());
let mut fn_ty = FnType::unadjusted(ccx, abi, &sig, extra_args);
if let Virtual(_) = self.data {
// Don't pass the vtable, it's not an argument of the virtual fn.
Expand Down Expand Up @@ -327,8 +326,7 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
bare_fn_ty);
}
};
let sig = tcx.erase_late_bound_regions(sig);
let sig = ccx.tcx().normalize_associated_type(&sig);
let sig = tcx.erase_late_bound_regions_and_normalize(sig);
let tuple_input_ty = tcx.mk_tup(sig.inputs.to_vec());
let sig = ty::FnSig {
inputs: vec![bare_fn_ty_maybe_ref,
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,

// Compute the rust-call form of the closure call method.
let sig = &tcx.closure_type(closure_id, substs).sig;
let sig = tcx.erase_late_bound_regions(sig);
let sig = tcx.normalize_associated_type(&sig);
let sig = tcx.erase_late_bound_regions_and_normalize(sig);
let closure_type = tcx.mk_closure_from_closure_substs(closure_id, substs);
let function_type = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
unsafety: hir::Unsafety::Normal,
Expand Down Expand Up @@ -126,8 +125,7 @@ pub fn trans_closure_body_via_mir<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
// of the closure expression.

let sig = &tcx.closure_type(closure_def_id, closure_substs).sig;
let sig = tcx.erase_late_bound_regions(sig);
let sig = tcx.normalize_associated_type(&sig);
let sig = tcx.erase_late_bound_regions_and_normalize(sig);

let closure_type = tcx.mk_closure_from_closure_substs(closure_def_id,
closure_substs);
Expand Down Expand Up @@ -249,8 +247,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
assert_eq!(abi, Abi::RustCall);
sig.0.inputs[0] = closure_ty;

let sig = tcx.erase_late_bound_regions(&sig);
let sig = tcx.normalize_associated_type(&sig);
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
let fn_ty = FnType::new(ccx, abi, &sig, &[]);

let llonce_fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ impl<'tcx> TypeMap<'tcx> {
ty::TyTrait(ref trait_data) => {
unique_type_id.push_str("trait ");

let principal = cx.tcx().erase_late_bound_regions(&trait_data.principal);
let principal = cx.tcx().erase_late_bound_regions_and_normalize(
&trait_data.principal);

from_def_id_and_substs(self,
cx,
Expand All @@ -254,8 +255,7 @@ impl<'tcx> TypeMap<'tcx> {

unique_type_id.push_str(" fn(");

let sig = cx.tcx().erase_late_bound_regions(sig);
let sig = cx.tcx().normalize_associated_type(&sig);
let sig = cx.tcx().erase_late_bound_regions_and_normalize(sig);

for &parameter_type in &sig.inputs {
let parameter_type_id =
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
output.push(']');
},
ty::TyTrait(ref trait_data) => {
let principal = cx.tcx().erase_late_bound_regions(&trait_data.principal);
let principal = cx.tcx().erase_late_bound_regions_and_normalize(
&trait_data.principal);
push_item_name(cx, principal.def_id, false, output);
push_type_params(cx, principal.substs, output);
},
Expand All @@ -112,8 +113,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,

output.push_str("fn(");

let sig = cx.tcx().erase_late_bound_regions(sig);
let sig = cx.tcx().normalize_associated_type(&sig);
let sig = cx.tcx().erase_late_bound_regions_and_normalize(sig);
if !sig.inputs.is_empty() {
for &parameter_type in &sig.inputs {
push_debuginfo_type_name(cx, parameter_type, true, output);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ pub fn declare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
fn_type: ty::Ty<'tcx>) -> ValueRef {
debug!("declare_rust_fn(name={:?}, fn_type={:?})", name, fn_type);
let abi = fn_type.fn_abi();
let sig = ccx.tcx().erase_late_bound_regions(fn_type.fn_sig());
let sig = ccx.tcx().normalize_associated_type(&sig);
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(fn_type.fn_sig());
debug!("declare_rust_fn (after region erasure) sig={:?}", sig);

let fty = FnType::new(ccx, abi, &sig, &[]);
Expand Down
12 changes: 5 additions & 7 deletions src/librustc_trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,12 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,

let _icx = push_ctxt("trans_intrinsic_call");

let (def_id, substs, sig) = match callee_ty.sty {
ty::TyFnDef(def_id, substs, fty) => {
let sig = tcx.erase_late_bound_regions(&fty.sig);
(def_id, substs, tcx.normalize_associated_type(&sig))
}
let (def_id, substs, fty) = match callee_ty.sty {
ty::TyFnDef(def_id, substs, ref fty) => (def_id, substs, fty),
_ => bug!("expected fn item type, found {}", callee_ty)
};

let sig = tcx.erase_late_bound_regions_and_normalize(&fty.sig);
let arg_tys = sig.inputs;
let ret_ty = sig.output;
let name = tcx.item_name(def_id).as_str();
Expand Down Expand Up @@ -1108,8 +1107,7 @@ fn generic_simd_intrinsic<'blk, 'tcx, 'a>


let tcx = bcx.tcx();
let sig = tcx.erase_late_bound_regions(callee_ty.fn_sig());
let sig = tcx.normalize_associated_type(&sig);
let sig = tcx.erase_late_bound_regions_and_normalize(callee_ty.fn_sig());
let arg_tys = sig.inputs;

// every intrinsic takes a SIMD vector as its first argument
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ pub fn trans_object_shim<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
_ => bug!()
};

let sig = tcx.erase_late_bound_regions(sig);
let sig = tcx.normalize_associated_type(&sig);
let sig = tcx.erase_late_bound_regions_and_normalize(sig);
let fn_ty = FnType::new(ccx, abi, &sig, &[]);

let llfn = declare::define_internal_fn(ccx, &function_name, callee.ty);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
_ => bug!("{} is not callable", callee.ty)
};

let sig = bcx.tcx().erase_late_bound_regions(sig);
let sig = bcx.tcx().erase_late_bound_regions_and_normalize(sig);

// Handle intrinsics old trans wants Expr's for, ourselves.
let intrinsic = match (&callee.ty.sty, &callee.data) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ pub fn push_unique_type_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

output.push_str("fn(");

let sig = tcx.erase_late_bound_regions(sig);
let sig = tcx.erase_late_bound_regions_and_normalize(sig);
if !sig.inputs.is_empty() {
for &parameter_type in &sig.inputs {
push_unique_type_name(tcx, parameter_type, output);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) ->

ty::TyFnDef(..) => Type::nil(cx),
ty::TyFnPtr(f) => {
let sig = cx.tcx().erase_late_bound_regions(&f.sig);
let sig = cx.tcx().normalize_associated_type(&sig);
let sig = cx.tcx().erase_late_bound_regions_and_normalize(&f.sig);
FnType::new(cx, f.abi, &sig, &[]).llvm_type(cx).ptr_to()
}
ty::TyTuple(ref tys) if tys.is_empty() => Type::nil(cx),
Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/issue-37109.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

trait ToRef<'a> {
type Ref: 'a;
}

impl<'a, U: 'a> ToRef<'a> for U {
type Ref = &'a U;
}

fn example<'a, T>(value: &'a T) -> (<T as ToRef<'a>>::Ref, u32) {
(value, 0)
}

fn main() {
example(&0);
}

0 comments on commit ee338c3

Please sign in to comment.