Skip to content

Commit 7c9b41a

Browse files
Rollup merge of rust-lang#53960 - estebank:issue-51303, r=nagisa
Fix incorrect outer function type parameter message Fix rust-lang#51303.
2 parents 1e5e014 + bebecf8 commit 7c9b41a

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

src/librustc_resolve/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,25 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver,
198198

199199
let cm = resolver.session.source_map();
200200
match outer_def {
201-
Def::SelfTy(_, maybe_impl_defid) => {
202-
if let Some(impl_span) = maybe_impl_defid.map_or(None,
203-
|def_id| resolver.definitions.opt_span(def_id)) {
204-
err.span_label(reduce_impl_span_to_impl_keyword(cm, impl_span),
205-
"`Self` type implicitly declared here, on the `impl`");
201+
Def::SelfTy(maybe_trait_defid, maybe_impl_defid) => {
202+
if let Some(impl_span) = maybe_impl_defid.and_then(|def_id| {
203+
resolver.definitions.opt_span(def_id)
204+
}) {
205+
err.span_label(
206+
reduce_impl_span_to_impl_keyword(cm, impl_span),
207+
"`Self` type implicitly declared here, by this `impl`",
208+
);
209+
}
210+
match (maybe_trait_defid, maybe_impl_defid) {
211+
(Some(_), None) => {
212+
err.span_label(span, "can't use `Self` here");
213+
}
214+
(_, Some(_)) => {
215+
err.span_label(span, "use a type here instead");
216+
}
217+
(None, None) => bug!("`impl` without trait nor type?"),
206218
}
219+
return err;
207220
},
208221
Def::TyParam(typaram_defid) => {
209222
if let Some(typaram_span) = resolver.definitions.opt_span(typaram_defid) {

src/test/ui/error-codes/E0401.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ error[E0401]: can't use type parameters from outer function
2424
--> $DIR/E0401.rs:32:25
2525
|
2626
LL | impl<T> Iterator for A<T> {
27-
| ---- `Self` type implicitly declared here, on the `impl`
27+
| ---- `Self` type implicitly declared here, by this `impl`
2828
...
2929
LL | fn helper(sel: &Self) -> u8 { //~ ERROR E0401
30-
| ------ ^^^^ use of type variable from outer function
31-
| |
32-
| help: try using a local type parameter instead: `helper<Self>`
30+
| ^^^^
31+
| |
32+
| use of type variable from outer function
33+
| use a type here instead
3334

3435
error: aborting due to 3 previous errors
3536

src/test/ui/issues/issue-12796.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0401]: can't use type parameters from outer function
22
--> $DIR/issue-12796.rs:13:22
33
|
44
LL | fn inner(_: &Self) {
5-
| ----- ^^^^ use of type variable from outer function
6-
| |
7-
| help: try using a local type parameter instead: `inner<Self>`
5+
| ^^^^
6+
| |
7+
| use of type variable from outer function
8+
| can't use `Self` here
89

910
error: aborting due to previous error
1011

src/test/ui/use-self-in-inner-fn.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 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+
struct A;
12+
13+
impl A {
14+
//~^ NOTE `Self` type implicitly declared here, by this `impl`
15+
fn banana(&mut self) {
16+
fn peach(this: &Self) {
17+
//~^ ERROR can't use type parameters from outer function
18+
//~| NOTE use of type variable from outer function
19+
//~| NOTE use a type here instead
20+
}
21+
}
22+
}
23+
24+
fn main() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0401]: can't use type parameters from outer function
2+
--> $DIR/use-self-in-inner-fn.rs:16:25
3+
|
4+
LL | impl A {
5+
| ---- `Self` type implicitly declared here, by this `impl`
6+
...
7+
LL | fn peach(this: &Self) {
8+
| ^^^^
9+
| |
10+
| use of type variable from outer function
11+
| use a type here instead
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0401`.

0 commit comments

Comments
 (0)