Skip to content

Commit 522d4b0

Browse files
committed
Fixed regression in associated item resolution with default type parameters that reference Self in traits.
1 parent fd230ff commit 522d4b0

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,20 @@ fn create_substs_for_ast_path<'tcx>(
413413
let mut type_substs = if param_mode == PathParamMode::Optional &&
414414
types_provided.is_empty() {
415415
let mut substs = region_substs.clone();
416+
416417
ty_param_defs
417418
.iter()
418-
.map(|p| this.ty_infer(Some(p.clone()), Some(&mut substs), Some(TypeSpace), span))
419+
.map(|p| {
420+
if let Some(ref default) = p.default {
421+
if self_ty.is_none() && default.has_self_ty() {
422+
// There is no suitable inference default for a type parameter
423+
// that references Self with no self-type provided.
424+
return this.ty_infer(None, Some(&mut substs), Some(TypeSpace), span);
425+
}
426+
}
427+
428+
this.ty_infer(Some(p.clone()), Some(&mut substs), Some(TypeSpace), span)
429+
})
419430
.collect()
420431
} else {
421432
types_provided

src/test/compile-fail/issue-28344.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
use std::ops::BitXor;
12+
13+
fn main() {
14+
let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
15+
//~^ ERROR must be specified
16+
//~| no associated item named
17+
18+
let g = BitXor::bitor;
19+
//~^ ERROR must be specified
20+
//~| no associated item named
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
pub trait Foo<A=Self> {
12+
fn foo();
13+
}
14+
15+
pub trait Bar<X=usize, A=Self> {
16+
fn foo();
17+
}
18+
19+
fn main() {
20+
let a = Foo::lol();
21+
//~^ ERROR no associated item named
22+
let b = Foo::<_>::lol();
23+
//~^ ERROR no associated item named
24+
let c = Bar::lol();
25+
//~^ ERROR no associated item named
26+
let d = Bar::<usize, _>::lol();
27+
//~^ ERROR no associated item named
28+
}

0 commit comments

Comments
 (0)