Skip to content

Add long error diagnostics for E0401 #29916

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

Merged
merged 1 commit into from
Nov 21, 2015
Merged
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
72 changes: 71 additions & 1 deletion src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,77 @@ on this topic:
https://doc.rust-lang.org/reference.html#use-declarations
"##,

E0401: r##"
Inner functions do not inherit type parameters from the functions they are
embedded in. For example, this will not compile:

```
fn foo<T>(x: T) {
fn bar(y: T) { // T is defined in the "outer" function
// ..
}
bar(x);
}
```

Functions inside functions are basically just like top-level functions, except
that they can only be called from the function they are in.

There are a couple of solutions for this.

You can use a closure:

```
fn foo<T>(x: T) {
let bar = |y: T| { // explicit type annotation may not be necessary
// ..
}
bar(x);
}
```

or copy over the parameters:

```
fn foo<T>(x: T) {
fn bar<T>(y: T) {
// ..
}
bar(x);
}
```

Be sure to copy over any bounds as well:

```
fn foo<T: Copy>(x: T) {
fn bar<T: Copy>(y: T) {
// ..
}
bar(x);
}
```

This may require additional type hints in the function body.

In case the function is in an `impl`, defining a private helper function might
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl block maybe ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really a block

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you call it ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just call it an impl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear people say both.

be easier:

```
impl<T> Foo<T> {
pub fn foo(&self, x: T) {
self.bar(x);
}
fn bar(&self, y: T) {
// ..
}
}
```

For default impls in traits, the private helper solution won't work, however
closures or copying the parameters should still work.
"##,

E0403: r##"
Some type parameters have the same name. Example of erroneous code:

Expand Down Expand Up @@ -909,7 +980,6 @@ register_diagnostics! {
E0254, // import conflicts with imported crate in this module
E0257,
E0258,
E0401, // can't use type parameters from outer function
E0402, // cannot use an outer type parameter in this context
E0406, // undeclared associated type
E0408, // variable from pattern #1 is not bound in pattern #
Expand Down