Skip to content

Commit

Permalink
Fix DeBruijn accounting. It used to be that all trait-refs were binders,
Browse files Browse the repository at this point in the history
but now only poly-trait-refs are binders. Fixes rust-lang#20831.
  • Loading branch information
nikomatsakis committed Jan 13, 2015
1 parent e94a9f0 commit 724ca86
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,15 @@ pub fn instantiate_poly_trait_ref<'tcx>(
{
let mut projections = Vec::new();

// the trait reference introduces a binding level here, so
// we need to shift the `rscope`. It'd be nice if we could
// do away with this rscope stuff and work this knowledge
// into resolve_lifetimes, as we do with non-omitted
// lifetimes. Oh well, not there yet.
let shifted_rscope = ShiftedRscope::new(rscope);

let trait_ref =
instantiate_trait_ref(this, rscope, &ast_trait_ref.trait_ref,
instantiate_trait_ref(this, &shifted_rscope, &ast_trait_ref.trait_ref,
self_ty, Some(&mut projections));

for projection in projections.into_iter() {
Expand Down Expand Up @@ -573,13 +580,6 @@ fn ast_path_to_trait_ref<'a,'tcx>(
debug!("ast_path_to_trait_ref {:?}", path);
let trait_def = this.get_trait_def(trait_def_id);

// the trait reference introduces a binding level here, so
// we need to shift the `rscope`. It'd be nice if we could
// do away with this rscope stuff and work this knowledge
// into resolve_lifetimes, as we do with non-omitted
// lifetimes. Oh well, not there yet.
let shifted_rscope = ShiftedRscope::new(rscope);

let (regions, types, assoc_bindings) = match path.segments.last().unwrap().parameters {
ast::AngleBracketedParameters(ref data) => {
// For now, require that parenthetical notation be used
Expand All @@ -595,7 +595,7 @@ fn ast_path_to_trait_ref<'a,'tcx>(
the crate attributes to enable");
}

convert_angle_bracketed_parameters(this, &shifted_rscope, data)
convert_angle_bracketed_parameters(this, rscope, data)
}
ast::ParenthesizedParameters(ref data) => {
// For now, require that parenthetical notation be used
Expand All @@ -616,7 +616,7 @@ fn ast_path_to_trait_ref<'a,'tcx>(
};

let substs = create_substs_for_ast_path(this,
&shifted_rscope,
rscope,
path.span,
&trait_def.generics,
self_ty,
Expand Down
43 changes: 43 additions & 0 deletions src/test/compile-fail/issue-20831-debruijn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 <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.

// Regression test for #20831: debruijn index account was thrown off
// by the (anonymous) lifetime in `<Self as Publisher>::Output`
// below. Note that changing to a named lifetime made the problem go
// away.

use std::ops::{Shl, Shr};
use std::cell::RefCell;

pub trait Subscriber {
type Input;
}

pub trait Publisher<'a> {
type Output;
fn subscribe(&mut self, Box<Subscriber<Input=Self::Output> + 'a>);
}

pub trait Processor<'a> : Subscriber + Publisher<'a> { }

impl<'a, P> Processor<'a> for P where P : Subscriber + Publisher<'a> { }

struct MyStruct<'a> {
sub: Box<Subscriber<Input=u64> + 'a>
}

impl<'a> Publisher<'a> for MyStruct<'a> {
type Output = u64;
fn subscribe(&mut self, t : Box<Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
self.subscriber = t;
}
}

fn main() {}

0 comments on commit 724ca86

Please sign in to comment.