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

rustc --explain E0744 says "while" is forbidden but accepts it nontheless #79083

Closed
karolherbst opened this issue Nov 15, 2020 · 4 comments · Fixed by #79087
Closed

rustc --explain E0744 says "while" is forbidden but accepts it nontheless #79083

karolherbst opened this issue Nov 15, 2020 · 4 comments · Fixed by #79087
Assignees
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@karolherbst
Copy link

karolherbst commented Nov 15, 2020

I have to use some sort of loop construct within a const function. rustc --explain E0744 does explain that loop constructs are at the moment not supported, but as I was trying out for first and the compiler rejected it, it would have been helpful if the explanation would hint me towards while working.

I think the doc just needs a small update and I think the file to update is https://github.com/rust-lang/rust/blob/master/compiler/rustc_error_codes/src/error_codes/E0744.md

I tried this code:

const fn foo() -> [u8; 10] {
    let mut a = [0; 10];
    let b = b"hi";
    let mut i = 0;
    while i < b.len() {
        a[i] = b[i];
        i += 1;
    }
    
    a
}

pub const BAR: [u8; 10] = foo();

Meta

rustc --version --verbose:

rustc 1.47.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0
@karolherbst karolherbst added the C-bug Category: This is a bug. label Nov 15, 2020
@ThePuzzlemaker
Copy link
Contributor

@rustbot claim

@ThePuzzlemaker
Copy link
Contributor

if, match, and loop are also allowed, but for is not.

@jyn514 jyn514 added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools labels Nov 16, 2020
@jyn514
Copy link
Member

jyn514 commented Nov 16, 2020

@karolherbst if it helps, the reason for is forbidden but the others work is that for uses IntoIterator and traits in const functions are still unstable (search for 'for loops' on https://rust-lang.github.io/const-eval/).

@karolherbst
Copy link
Author

@karolherbst if it helps, the reason for is forbidden but the others work is that for uses IntoIterator and traits in const functions are still unstable (search for 'for loops' on https://rust-lang.github.io/const-eval/).

yeah, that's understandable. The confusion on my side was simply that I didn't know I was allowed to user other control flow constructs until somebody mentioned it on IRC I actually could.

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, covers all const contexts (static, const fn, ...) A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
3 participants