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

Allow int literals for pattern types with int base types #137715

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Feb 27, 2025

r? @BoxyUwU

I also added an error at layout computation time for layouts that contain wrapping ranges (happens at monomorphization time). This is obviously hacky, but at least prevents such types from making it to codegen for now. It made writing the tests for int literals easier as I didn't have to think about that edge case

Basically this PR allows you to stop using transmutes for creating pattern types and instead just use literals:

let x: pattern_type!(u32 is 5..10) = 7;

works, and if the literal is out of range you get a type mismatch because it just stays at the base type and the base type can't be coerced to the pattern type.

cc @joshtriplett @scottmcm

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 27, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 27, 2025

changes to the core type system

cc @compiler-errors, @lcnr

HIR ty lowering was modified

cc @fmease

changes to the core type system

cc @compiler-errors, @lcnr

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@oli-obk oli-obk force-pushed the pattern-type-literals branch 2 times, most recently from 2845be6 to 897ace8 Compare March 5, 2025 09:11
@bors

This comment was marked as resolved.

@oli-obk oli-obk force-pushed the pattern-type-literals branch 2 times, most recently from e5f8770 to e9f5d48 Compare March 7, 2025 09:48
Comment on lines 1663 to 1665
if layout.uninhabited {
None
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patterns types of integers cant be uninhabited can they?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if they were why would we want to fallback to an integer var instead of using the expected (uninhabited) pattern type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if u32 is 10..2 exists, but if it does then it's uninhabited.

(And it might be nice to just allow the type to exist so that we don't need where {MIN <= MAX} bounds on things.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR makes us error when encountering wrapping ranges like 10..2 so that wouldn't be uninhabited as of this PR. Though if it did exist I could imagine it being uninhabited

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops yea that was just a leftover from doing various pattern types things in parallel. I turned it into an assert

@BoxyUwU BoxyUwU added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 7, 2025
Comment on lines +26 to +29
fn negative_lit_in_range() -> pattern_type!(i8 is -5..5) {
-2
//~^ ERROR: cannot apply unary operator `-` to type `(i8) is -5..=4`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something you expect to start supporting with your other work about having negated literals explicit in the ast/hir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's why I started that work

@oli-obk oli-obk force-pushed the pattern-type-literals branch from e9f5d48 to f87e58f Compare March 10, 2025 09:37
@oli-obk oli-obk added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants