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

Pattern types: Avoid having to handle an Option for range ends in the type system or the HIR #136922

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 12, 2025

Instead,

  1. during hir_ty_lowering, we now generate constants for the min/max when the range doesn't have a start/end specified.
  2. in a later commit we generate those constants during ast lowering, simplifying everything further by not having to handle the range end inclusivity anymore in the type system (and thus avoiding any issues of 0..5 being different from 0..=4

I think it makes all the type system code simpler, and the cost of the extra ConstKind::Value processing seems negligible.

r? @BoxyUwU

cc @joshtriplett @scottmcm

@rustbot
Copy link
Collaborator

rustbot commented Feb 12, 2025

changes to the core type system

cc @compiler-errors, @lcnr

changes to the core type system

cc @compiler-errors, @lcnr

HIR ty lowering was modified

cc @fmease

@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. labels Feb 12, 2025
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the pattern-types-option-ends branch 2 times, most recently from 6b71f0c to 938cccd Compare February 13, 2025 14:13
@oli-obk oli-obk changed the title Pattern types: Avoid having to handle an Option for range ends in the type system Pattern types: Avoid having to handle an Option for range ends in the type system or the HIR Feb 13, 2025
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from 938cccd to 2e3bbdd Compare February 13, 2025 14:42
@rustbot
Copy link
Collaborator

rustbot commented Feb 13, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from 2e3bbdd to d979188 Compare February 13, 2025 16:41
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from d979188 to ffd9b90 Compare February 14, 2025 08:37
@bors

This comment was marked as resolved.

@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from ffd9b90 to 7da1159 Compare February 20, 2025 13:41
@BoxyUwU

This comment was marked as outdated.

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 25, 2025

Something I was thinking about when reviewing this was whether this "normalization" of patterns to always be inclusive w/ both ends specified is something that can be done even when we start to support patterns such as N..M.

One thing that definitely wont be supported is continuing to lower to an anon const of the form { <T as RangePattern>::sub_one(...) } as generic parameters in anon consts work very poorly. I think, there are ~three ways that we can make it work eventually though:

  1. Wait for generic_const_expressions to be redesigned in terms of an extension to min_generic_const_args. From there we can make sub_one an associated constant. Something like <T as Trait<N>>::SUB_ONE. If we have a where clause on that assoc const that requires lower < N (required for lower..N to be a valid pattern type) then we can ensure ctfe won't kill the compilation session when evaluating <u32 as Trait<N>>::SUB_ONE.
  2. Hack up our hir/ty::Const repr to have a builtin version of SUB_ONE. Some kind of hir::ConstArgKind::SubOne/ty::ConstKind::Expr(ExprKind::SubOne. We can make type system normalization special case normalization of ExprKind::SubOne to check the value is >0 and then if it is just subtract one 🤷‍♀️ Eventually this can likely be replaced with a gce based hack once that exists
  3. Introduce some kind of UnboundedInt type with no upper/lower bound on what integers it can store. This would allow addition and subtraction to be a pure function that always terminates. We could then make range patterns of integers use such a type so that u8 is -1..40000 is a valid type.

Implying the right where clauses such that 2..=N-1 and 2..N are both able to be proven wf is maybe a bit tricky but I expect to be very doable. I also think that having to deal with optionally open ended and inclusive vs exclusive ranges in the type system is likely to be rather annoying so this is refactor is pretty important 🤔

@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 Feb 25, 2025
@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from 7da1159 to 16d3644 Compare February 26, 2025 10:36
@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from 16d3644 to dd0231e Compare February 26, 2025 10:43
@oli-obk oli-obk force-pushed the pattern-types-option-ends branch from dd0231e to 73b7e5f Compare February 26, 2025 11:07
@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 Feb 26, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 26, 2025

@bors r+

@bors
Copy link
Contributor

bors commented Feb 26, 2025

📌 Commit 73b7e5f has been approved by BoxyUwU

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 26, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 27, 2025
… r=BoxyUwU

Pattern types: Avoid having to handle an Option for range ends in the type system or the HIR

Instead,

1. during hir_ty_lowering, we now generate constants for the min/max when the range doesn't have a start/end specified.
2. in a later commit we generate those constants during ast lowering, simplifying everything further by not having to handle the range end inclusivity anymore in the type system (and thus avoiding any issues of `0..5` being different from `0..=4`

I think it makes all the type system code simpler, and the cost of the extra `ConstKind::Value` processing seems negligible.

r? `@BoxyUwU`

cc `@joshtriplett` `@scottmcm`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler 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