Skip to content

Commit 115a443

Browse files
committed
Normalize types in supertraits when checking that impls are valid during wf.
Fixes #20559.
1 parent bb0c8ef commit 115a443

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/librustc/middle/traits/project.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,11 @@ pub struct Normalized<'tcx,T> {
244244
pub type NormalizedTy<'tcx> = Normalized<'tcx, Ty<'tcx>>;
245245

246246
impl<'tcx,T> Normalized<'tcx,T> {
247-
fn with<U>(self, value: U) -> Normalized<'tcx,U> {
247+
pub fn with<U>(self, value: U) -> Normalized<'tcx,U> {
248248
Normalized { value: value, obligations: self.obligations }
249249
}
250250
}
251251

252-
253252
pub fn normalize_projection_type<'a,'b,'tcx>(
254253
selcx: &'a mut SelectionContext<'b,'tcx>,
255254
projection_ty: ty::ProjectionTy<'tcx>,

src/librustc_typeck/check/wf.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,16 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
235235
// Find the supertrait bounds. This will add `int:Bar`.
236236
let poly_trait_ref = ty::Binder(trait_ref);
237237
let predicates = ty::predicates_for_trait_ref(fcx.tcx(), &poly_trait_ref);
238-
for predicate in predicates.into_iter() {
238+
let predicates = {
239+
let selcx = &mut traits::SelectionContext::new(fcx.infcx(), fcx);
240+
traits::normalize(selcx, cause.clone(), &predicates)
241+
};
242+
for predicate in predicates.value.into_iter() {
239243
fcx.register_predicate(traits::Obligation::new(cause.clone(), predicate));
240244
}
245+
for obligation in predicates.obligations.into_iter() {
246+
fcx.register_predicate(obligation);
247+
}
241248
});
242249
}
243250
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2015 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+
// Test that we are handle to correctly handle a projection type
12+
// that appears in a supertrait bound. Issue #20559.
13+
14+
trait A
15+
{
16+
type TA;
17+
}
18+
19+
trait B<TB>
20+
{
21+
fn foo (&self, t : TB) -> String;
22+
}
23+
24+
trait C<TC : A> : B<<TC as A>::TA> { }
25+
26+
struct X;
27+
28+
impl A for X
29+
{
30+
type TA = i32;
31+
}
32+
33+
struct Y;
34+
35+
impl C<X> for Y { }
36+
37+
// Both of these impls are required for successful compilation
38+
impl B<i32> for Y
39+
{
40+
fn foo (&self, t : i32) -> String
41+
{
42+
format!("First {}", t)
43+
}
44+
}
45+
46+
fn main ()
47+
{
48+
let y = Y;
49+
assert_eq!(y.foo(5), format!("First 5"));
50+
}

0 commit comments

Comments
 (0)