Skip to content

Commit 0d707d1

Browse files
committed
Auto merge of #25485 - XuefengWu:24968_err_msg_parse_self_type_2, r=nrc
fix #24968 report more friendly error message for Self when fn args copy from #25096 r? @nrc @arielb1
2 parents d332aea + 7fe60c1 commit 0d707d1

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/librustc_resolve/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2361,8 +2361,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
23612361
"type name"
23622362
};
23632363

2364-
let msg = format!("use of undeclared {} `{}`", kind,
2365-
path_names_to_string(path, 0));
2364+
let self_type_name = special_idents::type_self.name;
2365+
let is_invalid_self_type_name =
2366+
path.segments.len() > 0 &&
2367+
maybe_qself.is_none() &&
2368+
path.segments[0].identifier.name == self_type_name;
2369+
let msg = if is_invalid_self_type_name {
2370+
"use of `Self` outside of an impl or trait".to_string()
2371+
} else {
2372+
format!("use of undeclared {} `{}`",
2373+
kind, path_names_to_string(path, 0))
2374+
};
2375+
23662376
self.resolve_error(ty.span, &msg[..]);
23672377
}
23682378
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait Trait {
1212
fn outer(self) {
1313
fn inner(_: Self) {
1414
//~^ ERROR can't use type parameters from outer function
15-
//~^^ ERROR use of undeclared type name `Self`
15+
//~^^ ERROR use of `Self` outside of an impl or trait
1616
}
1717
}
1818
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
fn foo(_: Self) {
12+
//~^ ERROR use of `Self` outside of an impl or trait
13+
}
14+
15+
fn main() {}

0 commit comments

Comments
 (0)