-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Fix ICE with unsized type in const pattern #87065
Merged
+65
−11
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Regression test for the ICE described in #87046. | ||
|
||
#![crate_type="lib"] | ||
#![allow(unreachable_patterns)] | ||
#![feature(const_fn_union)] | ||
|
||
#[derive(PartialEq, Eq)] | ||
#[repr(transparent)] | ||
pub struct Username(str); | ||
|
||
pub const ROOT_USER: &Username = Username::from_str("root"); | ||
|
||
impl Username { | ||
pub const fn from_str(raw: &str) -> &Self { | ||
union Transmute<'a> { | ||
raw: &'a str, | ||
typed: &'a Username, | ||
} | ||
|
||
unsafe { Transmute { raw }.typed } | ||
} | ||
|
||
pub const fn as_str(&self) -> &str { | ||
&self.0 | ||
} | ||
|
||
pub fn is_root(&self) -> bool { | ||
match self { | ||
ROOT_USER => true, | ||
//~^ ERROR: cannot use unsized non-slice type `Username` in constant patterns | ||
_ => false, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: cannot use unsized non-slice type `Username` in constant patterns | ||
--> $DIR/issue-87046.rs:29:13 | ||
| | ||
LL | ROOT_USER => true, | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that certainly avoids the ICE, but we could also make such patterns legal. I think we should. @rust-lang/wg-const-eval what do you think about these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This always does
span_err
ordelay_span_bug
... how does this make anything legal?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I said "we could ...", I'm fine doing this PR, it changes an ICE to an error, but why should the code in the new test not be permitted? It seems perfectly alright to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar enough with pattern matching code to comment on this.
But I'd prefer we port that code to valtrees first, then I will be a lot more confident that what that code does makes sense in my mental model of rustc. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oli-obk What would be a good way to direct attention to this? I would find it incredibly useful to be able to use newtypes around
str
as const patterns.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valtrees are being worked on right now, once we have them, this becomes trivial to do and we can ask T-lang whether they think this should br supported, too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool thanks