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

View File

@ -2,27 +2,27 @@
fn a<T: Clone>(x: T) {
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) {
let _ = move || {
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> {
fn a(x: T) {
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 {
fn a(x: T) {
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
|
LL | const foo: impl Clone = x;
| ^
|
= help: use the `|| { ... }` closure form instead
| ^ non-constant value
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
|
LL | const foo: impl Clone = x;
| ^
|
= help: use the `|| { ... }` closure form instead
| ^ non-constant value
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
|
LL | const foo: impl Clone = x;
| ^
|
= help: use the `|| { ... }` closure form instead
| ^ non-constant value
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
|
LL | const foo: impl Clone = x;
| ^
|
= help: use the `|| { ... }` closure form instead
| ^ non-constant value
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() {
let foo = 42u32;
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
|
LL | const FOO : u32 = foo;
| ^^^
|
= help: use the `|| { ... }` closure form instead
| ^^^ non-constant value
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;
static y: isize = foo + 1;
//~^ ERROR can't capture dynamic environment
//~^ ERROR attempt to use a non-constant value in a constant
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
|
LL | static y: isize = foo + 1;
| ^^^
|
= help: use the `|| { ... }` closure form instead
| ^^^ non-constant value
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) {
static child: isize = x + 1;
//~^ ERROR can't capture dynamic environment
//~^ ERROR attempt to use a non-constant value in a constant
}
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
|
LL | static child: isize = x + 1;
| ^
|
= help: use the `|| { ... }` closure form instead
| ^ non-constant value
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 {
fn getChildOption(&self) -> Option<Box<P>> {
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!();
}
}

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
|
LL | static childVal: Box<P> = self.child.get();
| ^^^^
|
= help: use the `|| { ... }` closure form instead
| ^^^^ non-constant value
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() {
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();
//~^ 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
|
LL | const l: usize = v.count(); //~ ERROR can't capture dynamic environment in a fn item
| ^
|
= help: use the `|| { ... }` closure form instead
LL | const l: usize = v.count(); //~ ERROR attempt to use a non-constant value in a constant
| ^ non-constant value
error[E0080]: evaluation of constant value failed
--> $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
Some errors occurred: E0080, E0434.
Some errors occurred: E0080, E0435.
For more information about an error, try `rustc --explain E0080`.