Skip to content
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

Make try_into const or don't suggest try_into in const scope #61065

Closed
estebank opened this issue May 23, 2019 · 2 comments
Closed

Make try_into const or don't suggest try_into in const scope #61065

estebank opened this issue May 23, 2019 · 2 comments
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given:

const fn foo() -> i32 {
    1
}

fn main() {
    [(); { foo() } ];
}

we suggest writing

#![feature(const_fn)]
use std::convert::TryInto;

const fn foo() -> i32 {
    1
}

fn main() {
    [(); { foo().try_into().unwrap() } ];
}

but when we do so, we complain about try_into not being const:

error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
 --> src/main.rs:9:12
  |
9 |     [(); { foo().try_into().unwrap() } ];
  |            ^^^^^^^^^^^^^^^^

error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
 --> src/main.rs:9:12
  |
9 |     [(); { foo().try_into().unwrap() } ];
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0080]: evaluation of constant value failed
 --> src/main.rs:9:12
  |
9 |     [(); { foo().try_into().unwrap() } ];
  |            ^^^^^^^^^^^^^^^^ calling non-const function `<i32 as std::convert::TryInto<usize>>::try_into`
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. A-const-eval Area: Constant evaluation (MIR interpretation) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels May 23, 2019
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 21, 2020
@estebank
Copy link
Contributor Author

We no longer suggest try_into:

error[E0308]: mismatched types
 --> src/main.rs:9:12
  |
9 |     [(); { foo() } ];
  |            ^^^^^ expected `usize`, found `i32`

@Trolldemorted
Copy link
Contributor

Could we have a const try_into and unwrap though? It would provide a very clean way of expressing "please don't compile if into fails", which improves code reliability on unusual and small target platforms.

We do have panic in const contexts, but we can't use it in traits (since trait methods can't be const yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. 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

3 participants