Skip to content

Commit

Permalink
Make name resolution handle consts in GenericParamsFromOuterFunction …
Browse files Browse the repository at this point in the history
…properly
  • Loading branch information
varkor committed Feb 7, 2019
1 parent 451f128 commit 4e0e188
Show file tree
Hide file tree
Showing 26 changed files with 120 additions and 93 deletions.
49 changes: 37 additions & 12 deletions src/librustc_resolve/lib.rs
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -4223,14 +4229,33 @@ impl<'a> Resolver<'a> {
resolve_error(
self,
span,
ResolutionError::TypeParametersFromOuterFunction(def),
ResolutionError::GenericParamsFromOuterFunction(def),
);
}
return Def::Err;
}
}
}
}
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
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_typeck/diagnostics.rs
Expand Up @@ -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:
```
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/parse/token.rs
Expand Up @@ -284,6 +284,7 @@ impl Token {
match self {
OpenDelim(Brace) => true,
Interpolated(ref nt) => match nt.0 {
NtExpr(..) => true,
NtBlock(..) => true,
NtLiteral(..) => true,
_ => false,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/bad/bad-type-env-capture.rs
@@ -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() { }
8 changes: 4 additions & 4 deletions src/test/ui/bad/bad-type-env-capture.stderr
@@ -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

Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/error-codes/E0401.stderr
@@ -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> {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/inner-static-type-parameter.rs
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/inner-static-type-parameter.stderr
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-12796.rs
@@ -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
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-12796.stderr
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-3021-c.rs
@@ -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
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/issues/issue-3021-c.stderr
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3214.rs
@@ -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> {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/issues/issue-3214.stderr
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-5997-enum.rs
@@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-5997-enum.stderr
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-5997-struct.rs
@@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/issues/issue-5997-struct.stderr
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nested-ty-params.rs
@@ -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]; }

Expand Down

0 comments on commit 4e0e188

Please sign in to comment.