Make name resolution handle consts in GenericParamsFromOuterFunction properly

This commit is contained in:
varkor 2019-02-07 14:59:59 +01:00
parent 451f128783
commit 4e0e188999
26 changed files with 120 additions and 93 deletions

View file

@ -142,8 +142,8 @@ impl Ord for BindingError {
}
enum ResolutionError<'a> {
/// error E0401: can't use type parameters from outer function
TypeParametersFromOuterFunction(Def),
/// error E0401: can't use type or const parameters from outer function
GenericParamsFromOuterFunction(Def),
/// error E0403: the name is already used for a type/const parameter in this list of
/// generic parameters
NameAlreadyUsedInParameterList(Name, &'a Span),
@ -196,13 +196,13 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
resolution_error: ResolutionError<'a>)
-> DiagnosticBuilder<'sess> {
match resolution_error {
ResolutionError::TypeParametersFromOuterFunction(outer_def) => {
ResolutionError::GenericParamsFromOuterFunction(outer_def) => {
let mut err = struct_span_err!(resolver.session,
span,
E0401,
"can't use type parameters from outer function",
"can't use generic parameters from outer function",
);
err.span_label(span, format!("use of type variable from outer function"));
err.span_label(span, format!("use of generic parameter from outer function"));
let cm = resolver.session.source_map();
match outer_def {
@ -231,15 +231,20 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
err.span_label(span, "type variable from outer function");
}
}
Def::ConstParam(def_id) => {
if let Some(span) = resolver.definitions.opt_span(def_id) {
err.span_label(span, "const variable from outer function");
}
}
_ => {
bug!("TypeParametersFromOuterFunction should only be used with Def::SelfTy, \
bug!("GenericParamsFromOuterFunction should only be used with Def::SelfTy, \
Def::TyParam");
}
}
// Try to retrieve the span of the function signature and generate a new message with
// a local type or const parameter.
let sugg_msg = &format!("try using a local type parameter instead");
let sugg_msg = &format!("try using a local generic parameter instead");
if let Some((sugg_span, new_snippet)) = cm.generate_local_type_param_snippet(span) {
// Suggest the modification to the user
err.span_suggestion(
@ -250,9 +255,9 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
);
} else if let Some(sp) = cm.generate_fn_name_span(span) {
err.span_label(sp,
format!("try adding a local type parameter in this method instead"));
format!("try adding a local generic parameter in this method instead"));
} else {
err.help(&format!("try using a local type parameter instead"));
err.help(&format!("try using a local generic parameter instead"));
}
err
@ -549,8 +554,7 @@ impl<'a> PathSource<'a> {
Def::Struct(..) | Def::Union(..) | Def::Enum(..) |
Def::Trait(..) | Def::TraitAlias(..) | Def::TyAlias(..) |
Def::AssociatedTy(..) | Def::PrimTy(..) | Def::TyParam(..) |
Def::SelfTy(..) | Def::Existential(..) | Def::ConstParam(..) |
Def::ForeignTy(..) => true,
Def::SelfTy(..) | Def::Existential(..) | Def::ForeignTy(..) => true,
_ => false,
},
PathSource::Trait(AliasPossibility::No) => match def {
@ -803,6 +807,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
_: Span,
node_id: NodeId)
{
debug!("(resolving function) entering function");
let (rib_kind, asyncness) = match function_kind {
FnKind::ItemFn(_, ref header, ..) =>
(ItemRibKind, header.asyncness),
@ -2053,6 +2058,7 @@ impl<'a> Resolver<'a> {
let record_used = record_used_id.is_some();
let mut module = self.graph_root;
for i in (0 .. self.ribs[ns].len()).rev() {
debug!("walk rib\n{:?}", self.ribs[ns][i].bindings);
if let Some(def) = self.ribs[ns][i].bindings.get(&ident).cloned() {
// The ident resolves to a type parameter or local variable.
return Some(LexicalScopeBinding::Def(
@ -4223,7 +4229,7 @@ impl<'a> Resolver<'a> {
resolve_error(
self,
span,
ResolutionError::TypeParametersFromOuterFunction(def),
ResolutionError::GenericParamsFromOuterFunction(def),
);
}
return Def::Err;
@ -4231,6 +4237,25 @@ impl<'a> Resolver<'a> {
}
}
}
Def::ConstParam(..) => {
// A const param is always declared in a signature, which is always followed by
// some kind of function rib kind (specifically, ItemRibKind in the case of a
// normal function), so we can skip the first rib as it will be guaranteed to
// (spuriously) conflict with the const param.
for rib in &ribs[1..] {
if let ItemRibKind = rib.kind {
// This was an attempt to use a const parameter outside its scope.
if record_used {
resolve_error(
self,
span,
ResolutionError::GenericParamsFromOuterFunction(def),
);
}
return Def::Err;
}
}
}
_ => {}
}
def

View file

@ -348,13 +348,14 @@ fn main() {
"##,
E0044: r##"
You can't use type parameters on foreign items. Example of erroneous code:
You can't use type or const parameters on foreign items.
Example of erroneous code:
```compile_fail,E0044
extern { fn some_func<T>(x: T); }
```
To fix this, replace the type parameter with the specializations that you
To fix this, replace the generic parameter with the specializations that you
need:
```

View file

@ -284,6 +284,7 @@ impl Token {
match self {
OpenDelim(Brace) => true,
Interpolated(ref nt) => match nt.0 {
NtExpr(..) => true,
NtBlock(..) => true,
NtLiteral(..) => true,
_ => false,
@ -306,7 +307,7 @@ impl Token {
}
}
/// Returns `true` if the token is any literal, a minus (which can follow a literal,
/// Returns `true` if the token is any literal, a minus (which can prefix a literal,
/// for example a '-42', or one of the boolean idents).
crate fn can_begin_literal_or_bool(&self) -> bool {
match *self {

View file

@ -1,4 +1,4 @@
fn foo<T>() {
fn bar(b: T) { } //~ ERROR can't use type parameters from outer
fn bar(b: T) { } //~ ERROR can't use generic parameters from outer
}
fn main() { }

View file

@ -1,12 +1,12 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/bad-type-env-capture.rs:2:15
|
LL | fn foo<T>() {
| - type variable from outer function
LL | fn bar(b: T) { } //~ ERROR can't use type parameters from outer
| --- ^ use of type variable from outer function
LL | fn bar(b: T) { } //~ ERROR can't use generic parameters from outer
| --- ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `bar<T>`
| help: try using a local generic parameter instead: `bar<T>`
error: aborting due to previous error

View file

@ -1,26 +1,26 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/E0401.rs:4:39
|
LL | fn foo<T>(x: T) {
| - type variable from outer function
LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) { //~ ERROR E0401
| --------------------------- ^ use of type variable from outer function
| --------------------------- ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `bfnr<U, V: Baz<U>, W: Fn(), T>`
| help: try using a local generic parameter instead: `bfnr<U, V: Baz<U>, W: Fn(), T>`
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/E0401.rs:9:16
|
LL | fn foo<T>(x: T) {
| - type variable from outer function
...
LL | fn baz<U,
| --- try adding a local type parameter in this method instead
| --- try adding a local generic parameter in this method instead
...
LL | (y: T) { //~ ERROR E0401
| ^ use of type variable from outer function
| ^ use of generic parameter from outer function
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/E0401.rs:22:25
|
LL | impl<T> Iterator for A<T> {
@ -29,7 +29,7 @@ LL | impl<T> Iterator for A<T> {
LL | fn helper(sel: &Self) -> u8 { //~ ERROR E0401
| ^^^^
| |
| use of type variable from outer function
| use of generic parameter from outer function
| use a type here instead
error: aborting due to 3 previous errors

View file

@ -4,7 +4,7 @@ enum Bar<T> { What } //~ ERROR parameter `T` is never used
fn foo<T>() {
static a: Bar<T> = Bar::What;
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
fn main() {

View file

@ -1,12 +1,12 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/inner-static-type-parameter.rs:6:19
|
LL | fn foo<T>() {
| --- - type variable from outer function
| |
| try adding a local type parameter in this method instead
| try adding a local generic parameter in this method instead
LL | static a: Bar<T> = Bar::What;
| ^ use of type variable from outer function
| ^ use of generic parameter from outer function
error[E0392]: parameter `T` is never used
--> $DIR/inner-static-type-parameter.rs:3:10

View file

@ -1,7 +1,7 @@
trait Trait {
fn outer(&self) {
fn inner(_: &Self) {
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
}
}

View file

@ -1,10 +1,10 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-12796.rs:3:22
|
LL | fn inner(_: &Self) {
| ^^^^
| |
| use of type variable from outer function
| use of generic parameter from outer function
| can't use `Self` here
error: aborting due to previous error

View file

@ -1,8 +1,8 @@
fn siphash<T>() {
trait U {
fn g(&self, x: T) -> T; //~ ERROR can't use type parameters from outer function
//~^ ERROR can't use type parameters from outer function
fn g(&self, x: T) -> T; //~ ERROR can't use generic parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
}

View file

@ -1,24 +1,24 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-3021-c.rs:4:24
|
LL | fn siphash<T>() {
| - type variable from outer function
...
LL | fn g(&self, x: T) -> T; //~ ERROR can't use type parameters from outer function
| - ^ use of type variable from outer function
LL | fn g(&self, x: T) -> T; //~ ERROR can't use generic parameters from outer function
| - ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `g<T>`
| help: try using a local generic parameter instead: `g<T>`
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-3021-c.rs:4:30
|
LL | fn siphash<T>() {
| - type variable from outer function
...
LL | fn g(&self, x: T) -> T; //~ ERROR can't use type parameters from outer function
| - ^ use of type variable from outer function
LL | fn g(&self, x: T) -> T; //~ ERROR can't use generic parameters from outer function
| - ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `g<T>`
| help: try using a local generic parameter instead: `g<T>`
error: aborting due to 2 previous errors

View file

@ -1,6 +1,6 @@
fn foo<T>() {
struct Foo {
x: T, //~ ERROR can't use type parameters from outer function
x: T, //~ ERROR can't use generic parameters from outer function
}
impl<T> Drop for Foo<T> {

View file

@ -1,13 +1,13 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-3214.rs:3:12
|
LL | fn foo<T>() {
| --- - type variable from outer function
| |
| try adding a local type parameter in this method instead
| try adding a local generic parameter in this method instead
LL | struct Foo {
LL | x: T, //~ ERROR can't use type parameters from outer function
| ^ use of type variable from outer function
LL | x: T, //~ ERROR can't use generic parameters from outer function
| ^ use of generic parameter from outer function
error[E0107]: wrong number of type arguments: expected 0, found 1
--> $DIR/issue-3214.rs:6:26

View file

@ -1,6 +1,6 @@
fn f<Z>() -> bool {
enum E { V(Z) }
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
true
}

View file

@ -1,12 +1,12 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-5997-enum.rs:2:16
|
LL | fn f<Z>() -> bool {
| - - type variable from outer function
| |
| try adding a local type parameter in this method instead
| try adding a local generic parameter in this method instead
LL | enum E { V(Z) }
| ^ use of type variable from outer function
| ^ use of generic parameter from outer function
error: aborting due to previous error

View file

@ -1,5 +1,5 @@
fn f<T>() -> bool {
struct S(T); //~ ERROR can't use type parameters from outer function
struct S(T); //~ ERROR can't use generic parameters from outer function
true
}

View file

@ -1,12 +1,12 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-5997-struct.rs:2:14
|
LL | fn f<T>() -> bool {
| - - type variable from outer function
| |
| try adding a local type parameter in this method instead
LL | struct S(T); //~ ERROR can't use type parameters from outer function
| ^ use of type variable from outer function
| try adding a local generic parameter in this method instead
LL | struct S(T); //~ ERROR can't use generic parameters from outer function
| ^ use of generic parameter from outer function
error: aborting due to previous error

View file

@ -1,4 +1,4 @@
// error-pattern:can't use type parameters from outer function
// error-pattern:can't use generic parameters from outer function
fn hd<U>(v: Vec<U> ) -> U {
fn hd1(w: [U]) -> U { return w[0]; }

View file

@ -1,22 +1,22 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/nested-ty-params.rs:3:16
|
LL | fn hd<U>(v: Vec<U> ) -> U {
| - type variable from outer function
LL | fn hd1(w: [U]) -> U { return w[0]; }
| --- ^ use of type variable from outer function
| --- ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `hd1<U>`
| help: try using a local generic parameter instead: `hd1<U>`
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/nested-ty-params.rs:3:23
|
LL | fn hd<U>(v: Vec<U> ) -> U {
| - type variable from outer function
LL | fn hd1(w: [U]) -> U { return w[0]; }
| --- ^ use of type variable from outer function
| --- ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `hd1<U>`
| help: try using a local generic parameter instead: `hd1<U>`
error: aborting due to 2 previous errors

View file

@ -6,7 +6,7 @@ trait TraitA<A> {
fn outer(&self) {
enum Foo<B> {
Variance(A)
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
}
}
@ -14,21 +14,21 @@ trait TraitA<A> {
trait TraitB<A> {
fn outer(&self) {
struct Foo<B>(A);
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
}
trait TraitC<A> {
fn outer(&self) {
struct Foo<B> { a: A }
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
}
trait TraitD<A> {
fn outer(&self) {
fn foo<B>(a: A) { }
//~^ ERROR can't use type parameters from outer function
//~^ ERROR can't use generic parameters from outer function
}
}

View file

@ -1,44 +1,44 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/resolve-type-param-in-item-in-trait.rs:8:22
|
LL | trait TraitA<A> {
| - type variable from outer function
LL | fn outer(&self) {
| ----- try adding a local type parameter in this method instead
| ----- try adding a local generic parameter in this method instead
LL | enum Foo<B> {
LL | Variance(A)
| ^ use of type variable from outer function
| ^ use of generic parameter from outer function
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/resolve-type-param-in-item-in-trait.rs:16:23
|
LL | trait TraitB<A> {
| - type variable from outer function
LL | fn outer(&self) {
| ----- try adding a local type parameter in this method instead
| ----- try adding a local generic parameter in this method instead
LL | struct Foo<B>(A);
| ^ use of type variable from outer function
| ^ use of generic parameter from outer function
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/resolve-type-param-in-item-in-trait.rs:23:28
|
LL | trait TraitC<A> {
| - type variable from outer function
LL | fn outer(&self) {
| ----- try adding a local type parameter in this method instead
| ----- try adding a local generic parameter in this method instead
LL | struct Foo<B> { a: A }
| ^ use of type variable from outer function
| ^ use of generic parameter from outer function
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/resolve-type-param-in-item-in-trait.rs:30:22
|
LL | trait TraitD<A> {
| - type variable from outer function
LL | fn outer(&self) {
LL | fn foo<B>(a: A) { }
| ------ ^ use of type variable from outer function
| ------ ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `foo<B, A>`
| help: try using a local generic parameter instead: `foo<B, A>`
error: aborting due to 4 previous errors

View file

@ -1,4 +1,4 @@
// error-pattern:can't use type parameters from outer function
// error-pattern:can't use generic parameters from outer function
fn foo<T>(x: T) {
fn bar(f: Box<FnMut(T) -> T>) { }
}

View file

@ -1,22 +1,22 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/type-arg-out-of-scope.rs:3:25
|
LL | fn foo<T>(x: T) {
| - type variable from outer function
LL | fn bar(f: Box<FnMut(T) -> T>) { }
| --- ^ use of type variable from outer function
| --- ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `bar<T>`
| help: try using a local generic parameter instead: `bar<T>`
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/type-arg-out-of-scope.rs:3:31
|
LL | fn foo<T>(x: T) {
| - type variable from outer function
LL | fn bar(f: Box<FnMut(T) -> T>) { }
| --- ^ use of type variable from outer function
| --- ^ use of generic parameter from outer function
| |
| help: try using a local type parameter instead: `bar<T>`
| help: try using a local generic parameter instead: `bar<T>`
error: aborting due to 2 previous errors

View file

@ -4,8 +4,8 @@ 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
//~^ ERROR can't use generic parameters from outer function
//~| NOTE use of generic parameter from outer function
//~| NOTE use a type here instead
}
}

View file

@ -1,4 +1,4 @@
error[E0401]: can't use type parameters from outer function
error[E0401]: can't use generic parameters from outer function
--> $DIR/use-self-in-inner-fn.rs:6:25
|
LL | impl A {
@ -7,7 +7,7 @@ LL | impl A {
LL | fn peach(this: &Self) {
| ^^^^
| |
| use of type variable from outer function
| use of generic parameter from outer function
| use a type here instead
error: aborting due to previous error