Skip to content

allow const function calls in consts that are used in patterns#30141

Merged
bors merged 3 commits into
rust-lang:masterfrom
oli-obk:fix/30117
Dec 16, 2015
Merged

allow const function calls in consts that are used in patterns#30141
bors merged 3 commits into
rust-lang:masterfrom
oli-obk:fix/30117

Conversation

@oli-obk

@oli-obk oli-obk commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

@rust-highfive

Copy link
Copy Markdown
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@oli-obk

oli-obk commented Dec 2, 2015

Copy link
Copy Markdown
Contributor Author

the reason this PR fixes two bugs, is that #29927 was also breaking the test for #30117

@arielb1

arielb1 commented Dec 2, 2015

Copy link
Copy Markdown
Contributor

@oli-obk

Why are you using PatLit? Will this work for more complicated const fn return results (that feels like a separate issue to me)?

@oli-obk

oli-obk commented Dec 2, 2015

Copy link
Copy Markdown
Contributor Author

it's already done for everything that is not a pattern directly. Like if you have an integral operation in a constant, it'll just defer it to PatLit and run the const evaluator later.

@oli-obk

oli-obk commented Dec 2, 2015

Copy link
Copy Markdown
Contributor Author

example: this won't work, since the enum is not directly in a const:

enum Cake {
    BlackForest,
    Marmor,
}
use Cake::*;

const BOO: (Cake, Cake) = (Marmor, BlackForest);
const FOO: Cake = BOO.1;

fn main() {
    match BlackForest {
        FOO => println!("hi"),
        _ => println!("bye"),
    }
}

@oli-obk

oli-obk commented Dec 2, 2015

Copy link
Copy Markdown
Contributor Author

The only expressions that are handled without the const evaluator are ExprTup, ExprCall, ExprStruct, ExprVec and ExprPath. Everything else is turned into a PatLit (see https://github.com/rust-lang/rust/pull/30141/files#diff-58f4817150d93d34d850a20426c38371L366) and then const evaluated here: https://github.com/rust-lang/rust/blob/master/src/librustc/middle/check_match.rs#L781

@arielb1

arielb1 commented Dec 2, 2015

Copy link
Copy Markdown
Contributor

@oli-obk

At least add a test that these kinds of abuse result in an error rather than an ICE.

@oli-obk

oli-obk commented Dec 2, 2015

Copy link
Copy Markdown
Contributor Author

added a test and noticed that the error is reported twice for the const fn. So now I'm bailing out in case the error was repeated previously.

do you mean "abuse" in the "you shouldn't do this" way or in the "complex situation" way?

@arielb1

arielb1 commented Dec 2, 2015

Copy link
Copy Markdown
Contributor

@oli-obk

According to @nikomatsakis we are going to change constant patterns to eliminate that possibility - but still we should not ICE.

@oli-obk

oli-obk commented Dec 3, 2015

Copy link
Copy Markdown
Contributor Author

I removed the commit that bailed early, check-stage1 passes locally, but travis didn't notice the removed commit.

@bors

bors commented Dec 4, 2015

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (presumably #30084) made this pull request unmergeable. Please resolve the merge conflicts.

@oli-obk

oli-obk commented Dec 5, 2015

Copy link
Copy Markdown
Contributor Author

rebased

@oli-obk

oli-obk commented Dec 15, 2015

Copy link
Copy Markdown
Contributor Author

I'm not sure what possibility is going to be eliminated, but the following already works, because it hits something (a tuple index op) that will be const evaluated:

#![feature(const_fn)]
const BOO: (i32, i64) = (5, bla());
const fn bla() -> i64 { 6 }
const FOO: i64 = BOO.1;

fn main() {
    match 6 {
        FOO => println!("hi"),
        _ => println!("bye"),
    }
}

all this PR does is to make sure the following also works

#![feature(const_fn)]
const fn bla() -> i64 { 6 }
const FOO: i64 = bla();

fn main() {
    match 6 {
        FOO => println!("hi"),
        _ => println!("bye"),
    }
}

@arielb1

arielb1 commented Dec 15, 2015

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Dec 15, 2015

Copy link
Copy Markdown
Collaborator

📌 Commit c71dcca has been approved by arielb1

bors added a commit that referenced this pull request Dec 16, 2015
@bors

bors commented Dec 16, 2015

Copy link
Copy Markdown
Collaborator

⌛ Testing commit c71dcca with merge 9ace0a4...

@bors bors merged commit c71dcca into rust-lang:master Dec 16, 2015
@oli-obk oli-obk deleted the fix/30117 branch December 16, 2015 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants