Resolve incorrect diagnostic for using a non-const value in a constant

This commit is contained in:
varkor 2019-02-07 16:03:12 +01:00
parent 1b933a5ce9
commit f2fe71c02a
13 changed files with 47 additions and 57 deletions

View File

@ -4164,6 +4164,9 @@ impl<'a> Resolver<'a> {
span_bug!(span, "unexpected {:?} in bindings", def) span_bug!(span, "unexpected {:?} in bindings", def)
} }
Def::Local(node_id) => { Def::Local(node_id) => {
use ResolutionError::*;
let mut res_err = None;
for rib in ribs { for rib in ribs {
match rib.kind { match rib.kind {
NormalRibKind | ModuleRibKind(..) | MacroDefinition(..) | NormalRibKind | ModuleRibKind(..) | MacroDefinition(..) |
@ -4199,21 +4202,26 @@ impl<'a> Resolver<'a> {
// named function item. This is not allowed, so we // named function item. This is not allowed, so we
// report an error. // report an error.
if record_used { if record_used {
resolve_error(self, span, // We don't immediately trigger a resolve error, because
ResolutionError::CannotCaptureDynamicEnvironmentInFnItem); // we want certain other resolution errors (namely those
// emitted for `ConstantItemRibKind` below) to take
// precedence.
res_err = Some(CannotCaptureDynamicEnvironmentInFnItem);
} }
return Def::Err;
} }
ConstantItemRibKind => { ConstantItemRibKind => {
// Still doesn't deal with upvars // Still doesn't deal with upvars
if record_used { if record_used {
resolve_error(self, span, resolve_error(self, span, AttemptToUseNonConstantValueInConstant);
ResolutionError::AttemptToUseNonConstantValueInConstant);
} }
return Def::Err; return Def::Err;
} }
} }
} }
if let Some(res_err) = res_err {
resolve_error(self, span, res_err);
return Def::Err;
}
} }
Def::TyParam(..) | Def::SelfTy(..) => { Def::TyParam(..) | Def::SelfTy(..) => {
for rib in ribs { for rib in ribs {

View File

@ -2,27 +2,27 @@
fn a<T: Clone>(x: T) { fn a<T: Clone>(x: T) {
const foo: impl Clone = x; const foo: impl Clone = x;
//~^ ERROR can't capture dynamic environment in a fn item //~^ ERROR attempt to use a non-constant value in a constant
} }
fn b<T: Clone>(x: T) { fn b<T: Clone>(x: T) {
let _ = move || { let _ = move || {
const foo: impl Clone = x; const foo: impl Clone = x;
//~^ ERROR can't capture dynamic environment in a fn item //~^ ERROR attempt to use a non-constant value in a constant
}; };
} }
trait Foo<T: Clone> { trait Foo<T: Clone> {
fn a(x: T) { fn a(x: T) {
const foo: impl Clone = x; const foo: impl Clone = x;
//~^ ERROR can't capture dynamic environment in a fn item //~^ ERROR attempt to use a non-constant value in a constant
} }
} }
impl<T: Clone> Foo<T> for i32 { impl<T: Clone> Foo<T> for i32 {
fn a(x: T) { fn a(x: T) {
const foo: impl Clone = x; const foo: impl Clone = x;
//~^ ERROR can't capture dynamic environment in a fn item //~^ ERROR attempt to use a non-constant value in a constant
} }
} }

View File

@ -1,35 +1,27 @@
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:4:29 --> $DIR/bindings.rs:4:29
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| ^ | ^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:10:33 --> $DIR/bindings.rs:10:33
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| ^ | ^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:17:33 --> $DIR/bindings.rs:17:33
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| ^ | ^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:24:33 --> $DIR/bindings.rs:24:33
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| ^ | ^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error: aborting due to 4 previous errors error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0434`. For more information about this error, try `rustc --explain E0435`.

View File

@ -1,5 +1,5 @@
fn main() { fn main() {
let foo = 42u32; let foo = 42u32;
const FOO : u32 = foo; const FOO : u32 = foo;
//~^ ERROR can't capture dynamic environment //~^ ERROR attempt to use a non-constant value in a constant
} }

View File

@ -1,11 +1,9 @@
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-27433.rs:3:23 --> $DIR/issue-27433.rs:3:23
| |
LL | const FOO : u32 = foo; LL | const FOO : u32 = foo;
| ^^^ | ^^^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0434`. For more information about this error, try `rustc --explain E0435`.

View File

@ -2,7 +2,7 @@ fn main() {
let foo = 100; let foo = 100;
static y: isize = foo + 1; static y: isize = foo + 1;
//~^ ERROR can't capture dynamic environment //~^ ERROR attempt to use a non-constant value in a constant
println!("{}", y); println!("{}", y);
} }

View File

@ -1,11 +1,9 @@
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3521-2.rs:4:23 --> $DIR/issue-3521-2.rs:4:23
| |
LL | static y: isize = foo + 1; LL | static y: isize = foo + 1;
| ^^^ | ^^^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0434`. For more information about this error, try `rustc --explain E0435`.

View File

@ -1,6 +1,6 @@
fn f(x:isize) { fn f(x:isize) {
static child: isize = x + 1; static child: isize = x + 1;
//~^ ERROR can't capture dynamic environment //~^ ERROR attempt to use a non-constant value in a constant
} }
fn main() {} fn main() {}

View File

@ -1,11 +1,9 @@
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3668-2.rs:2:27 --> $DIR/issue-3668-2.rs:2:27
| |
LL | static child: isize = x + 1; LL | static child: isize = x + 1;
| ^ | ^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0434`. For more information about this error, try `rustc --explain E0435`.

View File

@ -6,7 +6,7 @@ trait PTrait {
impl PTrait for P { impl PTrait for P {
fn getChildOption(&self) -> Option<Box<P>> { fn getChildOption(&self) -> Option<Box<P>> {
static childVal: Box<P> = self.child.get(); static childVal: Box<P> = self.child.get();
//~^ ERROR can't capture dynamic environment //~^ ERROR attempt to use a non-constant value in a constant
panic!(); panic!();
} }
} }

View File

@ -1,11 +1,9 @@
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3668.rs:8:34 --> $DIR/issue-3668.rs:8:34
| |
LL | static childVal: Box<P> = self.child.get(); LL | static childVal: Box<P> = self.child.get();
| ^^^^ | ^^^^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0434`. For more information about this error, try `rustc --explain E0435`.

View File

@ -1,6 +1,6 @@
fn main() { fn main() {
let v = vec![0]; let v = vec![0];
const l: usize = v.count(); //~ ERROR can't capture dynamic environment in a fn item const l: usize = v.count(); //~ ERROR attempt to use a non-constant value in a constant
let s: [u32; l] = v.into_iter().collect(); let s: [u32; l] = v.into_iter().collect();
//~^ ERROR evaluation of constant value failed //~^ ERROR evaluation of constant value failed
} }

View File

@ -1,10 +1,8 @@
error[E0434]: can't capture dynamic environment in a fn item error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/type-dependent-def-issue-49241.rs:3:22 --> $DIR/type-dependent-def-issue-49241.rs:3:22
| |
LL | const l: usize = v.count(); //~ ERROR can't capture dynamic environment in a fn item LL | const l: usize = v.count(); //~ ERROR attempt to use a non-constant value in a constant
| ^ | ^ non-constant value
|
= help: use the `|| { ... }` closure form instead
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/type-dependent-def-issue-49241.rs:4:18 --> $DIR/type-dependent-def-issue-49241.rs:4:18
@ -14,5 +12,5 @@ LL | let s: [u32; l] = v.into_iter().collect();
error: aborting due to 2 previous errors error: aborting due to 2 previous errors
Some errors occurred: E0080, E0434. Some errors occurred: E0080, E0435.
For more information about an error, try `rustc --explain E0080`. For more information about an error, try `rustc --explain E0080`.