Skip to content

Commit d7a74be

Browse files
committed
Fix incorrect outer function type parameter message
1 parent 1c2e17f commit d7a74be

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

src/librustc_resolve/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,15 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver,
197197
err.span_label(span, "use of type variable from outer function");
198198

199199
let cm = resolver.session.source_map();
200+
let mut is_self = false;
200201
match outer_def {
201202
Def::SelfTy(_, maybe_impl_defid) => {
202203
if let Some(impl_span) = maybe_impl_defid.map_or(None,
203204
|def_id| resolver.definitions.opt_span(def_id)) {
204205
err.span_label(reduce_impl_span_to_impl_keyword(cm, impl_span),
205206
"`Self` type implicitly declared here, on the `impl`");
206207
}
208+
is_self = true;
207209
},
208210
Def::TyParam(typaram_defid) => {
209211
if let Some(typaram_span) = resolver.definitions.opt_span(typaram_defid) {
@@ -219,7 +221,12 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver,
219221
// Try to retrieve the span of the function signature and generate a new message with
220222
// a local type parameter
221223
let sugg_msg = "try using a local type parameter instead";
222-
if let Some((sugg_span, new_snippet)) = cm.generate_local_type_param_snippet(span) {
224+
if is_self {
225+
// Suggest using the actual type
226+
err.span_label(span, "use a materialized type here instead");
227+
} else if let Some(
228+
(sugg_span, new_snippet),
229+
) = cm.generate_local_type_param_snippet(span) {
223230
// Suggest the modification to the user
224231
err.span_suggestion_with_applicability(
225232
sugg_span,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ LL | impl<T> Iterator for A<T> {
2727
| ---- `Self` type implicitly declared here, on the `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 materialized type here instead
3334

3435
error: aborting due to 3 previous errors
3536

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

Lines changed: 4 additions & 3 deletions
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+
| use a materialized type here instead
89

910
error: aborting due to previous error
1011

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

Lines changed: 24 additions & 0 deletions
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, on the `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 materialized type here instead
20+
}
21+
}
22+
}
23+
24+
fn main() {}
Lines changed: 15 additions & 0 deletions
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, on the `impl`
6+
...
7+
LL | fn peach(this: &Self) {
8+
| ^^^^
9+
| |
10+
| use of type variable from outer function
11+
| use a materialized 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)