Skip to content

Diagnostic: "constant evaluation error" without further details #41893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SimonSapin opened this issue May 10, 2017 · 13 comments
Closed

Diagnostic: "constant evaluation error" without further details #41893

SimonSapin opened this issue May 10, 2017 · 13 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SimonSapin
Copy link
Contributor

#![feature(nonzero, const_fn)]

extern crate core;

mod a {
    use core::nonzero::NonZero;

    #[derive(Eq, PartialEq)]
    pub struct Foo {
        bar: NonZero<u64>,  // Private
    }

    pub const FOO: Foo = Foo {
        bar: unsafe { NonZero::new(2) },
    };
}

fn main() {
    assert!(a::FOO == a::FOO);
    assert!(match a::FOO {
        a::FOO => true,
        _ => false,
    });
}

rustc 1.19.0-nightly (d3abc80 2017-05-09)

error[E0080]: constant evaluation error
   |
note: for pattern here
  --> a.rs:21:9
   |
21 |         a::FOO => true,
   |         ^^^^^^

error: aborting due to previous error

The error message doesn’t say what’s wrong with this pattern. Based on code in src/librustc/middle/const_val.rs it looks like more info should be printed, with ConstEvalErr::description.

CC @eddyb

@SimonSapin
Copy link
Contributor Author

I was able to learn more with:

diff --git a/src/librustc/middle/const_val.rs b/src/librustc/middle/const_val.rs
index 3bbaf5c929..7e98f25894 100644
--- a/src/librustc/middle/const_val.rs
+++ b/src/librustc/middle/const_val.rs
@@ -205,6 +205,7 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
             diag.span_note(primary_span,
                         &format!("for {} here", primary_kind));
         }
+        panic!("{:?}", self.description());
     }
 
     pub fn report(&self,

thread 'rustc' panicked at 'Simple("unimplemented constant expression: tuple struct constructors")', src/librustc/middle/const_val.rs:208

Let’s keep this issue about the error description not being shown. I’ll file a separate issue about the unimplemented bit.

Smaller test case with same results:

#![feature(nonzero, const_fn)]
extern crate core;
use core::nonzero::NonZero;

fn main() {
    const FOO: NonZero<u64> = unsafe { NonZero::new(2) };
    if let FOO = FOO {}
}

@oli-obk
Copy link
Contributor

oli-obk commented May 12, 2017

It's not related to const eval apparently, because

fn main() {
    const I: usize = 0 - 1;
    if let I = 42 {}
}

produces

error[E0080]: constant evaluation error
 --> <anon>:7:22
  |
7 |     const I: usize = 0 - 1;
  |                      ^^^^^ attempt to subtract with overflow
  |
note: for pattern here
 --> <anon>:8:12
  |
8 |     if let I = 42 {}
  |   

as expected. I assume it has something to do with the emitter itself.

Playpen: https://is.gd/RrPhcP

@Mark-Simulacrum
Copy link
Member

The span issue has been fixed -- closing.

error[E0080]: constant evaluation error
  --> /home/mark/BuildDisk/rust/src/libcore/nonzero.rs:47:9
   |
47 |         NonZero(inner)
   |         ^^^^^^^ unimplemented constant expression: tuple struct constructors
   |
note: for pattern here
  --> test.rs:21:9
   |
21 |         a::FOO => true,
   |         ^^^^^^

error: aborting due to previous error(s)

@SimonSapin
Copy link
Contributor Author

@Mark-Simulacrum What test case is this? On both test cases in this issue I still get “constant evaluation error” “for pattern here” without further details, on rustc 1.20.0-nightly (69c65d2 2017-06-28).

I think this should be re-opened.

@SimonSapin
Copy link
Contributor Author

Oh, I see that this the first test case, and the first line of code in the error message is from libcore. It also looks like you built this compiler yourself. Is the fix maybe on in Nightly yet? Or does the first part of the error message only show up when libcore sources are available at the same path they were compiled?

@eddyb
Copy link
Member

eddyb commented Jun 29, 2017

@SimonSapin Have you tried installing the rust-src component in rustup?

@SimonSapin
Copy link
Contributor Author

info: component 'rust-src' is up to date

It probably ends up at a different path than on the build servers, though? Does rustc know where to find it?

@SimonSapin
Copy link
Contributor Author

FWIW, I have a non-default $RUSTUP_HOME environment variable set.

@SimonSapin
Copy link
Contributor Author

Regardless, I think it’s a bug to skip half of the error message with relevant info when the corresponding source is not available to show context.

@Mark-Simulacrum
Copy link
Member

It might be a bug in rustc/rustup interaction; I'll reopen for now. I test with a locally compiled rustc.

@SimonSapin
Copy link
Contributor Author

Ok, so I’m convinced this is two issues:

  • rustc doesn’t know where to find the rust-src component from rustup. Perhaps a new environment variable would help?
  • When some sources are not available (which can happen for a variety of reasons), the error message omits valuable information that it would have printed in context next to a relevant source line. It should fall back to not printing the source context but still provide the information.

@eddyb
Copy link
Member

eddyb commented Jun 29, 2017

@SimonSapin Ah that second one is the result of the new label system... I find it a bit problematic in general, given the fact that we still have the "error headers" - we should only have one system IMO, and it should look like the current "error headers" with no source code (but still show the path, line & column for manual lookup).

As for finding rust-src... I guess that's tricky and rustup would have to provide a path somehow - while distros would probably hardcode their source directory in the compiler.

cc @rust-lang/dev-tools

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

I believe this is now a duplicate of #53081.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants