Skip to content

Commit

Permalink
Resolve incorrect diagnostic for using a non-const value in a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Feb 7, 2019
1 parent 1b933a5 commit f2fe71c
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 57 deletions.
18 changes: 13 additions & 5 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..) |
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/impl-trait/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
26 changes: 9 additions & 17 deletions src/test/ui/impl-trait/bindings.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-27433.rs
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 3 additions & 5 deletions src/test/ui/issues/issue-27433.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3521-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 3 additions & 5 deletions src/test/ui/issues/issue-3521-2.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3668-2.rs
Original file line number Diff line number Diff line change
@@ -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() {}
8 changes: 3 additions & 5 deletions src/test/ui/issues/issue-3668-2.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3668.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/test/ui/issues/issue-3668.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/type/type-dependent-def-issue-49241.rs
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 4 additions & 6 deletions src/test/ui/type/type-dependent-def-issue-49241.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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`.

0 comments on commit f2fe71c

Please sign in to comment.