Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect outer function type parameter message #53960

Merged
merged 3 commits into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,25 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver,

let cm = resolver.session.source_map();
match outer_def {
Def::SelfTy(_, maybe_impl_defid) => {
if let Some(impl_span) = maybe_impl_defid.map_or(None,
|def_id| resolver.definitions.opt_span(def_id)) {
err.span_label(reduce_impl_span_to_impl_keyword(cm, impl_span),
"`Self` type implicitly declared here, on the `impl`");
Def::SelfTy(maybe_trait_defid, maybe_impl_defid) => {
if let Some(impl_span) = maybe_impl_defid.and_then(|def_id| {
resolver.definitions.opt_span(def_id)
}) {
err.span_label(
reduce_impl_span_to_impl_keyword(cm, impl_span),
"`Self` type implicitly declared here, by this `impl`",
);
}
match (maybe_trait_defid, maybe_impl_defid) {
(Some(_), None) => {
err.span_label(span, "can't use `Self` here");
}
(_, Some(_)) => {
err.span_label(span, "use a type here instead");
}
(None, None) => bug!("`impl` without trait nor type?"),
}
return err;
},
Def::TyParam(typaram_defid) => {
if let Some(typaram_span) = resolver.definitions.opt_span(typaram_defid) {
Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/error-codes/E0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ error[E0401]: can't use type parameters from outer function
--> $DIR/E0401.rs:32:25
|
LL | impl<T> Iterator for A<T> {
| ---- `Self` type implicitly declared here, on the `impl`
| ---- `Self` type implicitly declared here, by this `impl`
...
LL | fn helper(sel: &Self) -> u8 { //~ ERROR E0401
| ------ ^^^^ use of type variable from outer function
| |
| help: try using a local type parameter instead: `helper<Self>`
| ^^^^
| |
| use of type variable from outer function
| use a type here instead

error: aborting due to 3 previous errors

Expand Down
7 changes: 4 additions & 3 deletions src/test/ui/issues/issue-12796.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ error[E0401]: can't use type parameters from outer function
--> $DIR/issue-12796.rs:13:22
|
LL | fn inner(_: &Self) {
| ----- ^^^^ use of type variable from outer function
| |
| help: try using a local type parameter instead: `inner<Self>`
| ^^^^
| |
| use of type variable from outer function
| can't use `Self` here

error: aborting due to previous error

Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/use-self-in-inner-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct A;

impl A {
//~^ NOTE `Self` type implicitly declared here, by this `impl`
fn banana(&mut self) {
fn peach(this: &Self) {
//~^ ERROR can't use type parameters from outer function
//~| NOTE use of type variable from outer function
//~| NOTE use a type here instead
}
}
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/use-self-in-inner-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0401]: can't use type parameters from outer function
--> $DIR/use-self-in-inner-fn.rs:16:25
|
LL | impl A {
| ---- `Self` type implicitly declared here, by this `impl`
...
LL | fn peach(this: &Self) {
| ^^^^
| |
| use of type variable from outer function
| use a type here instead

error: aborting due to previous error

For more information about this error, try `rustc --explain E0401`.