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

Prevent generic pattern types from being used in libstd #136584

Merged
merged 1 commit into from
Feb 11, 2025

Conversation

oli-obk
Copy link
Contributor

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

Pattern types should follow the same rules that patterns follow. So a pattern type range must not wrap and not be empty. While we reject such invalid ranges at layout computation time, that only happens during monomorphization in the case of const generics. This is the exact same issue as other const generic math has, and since there's no solution there yet, I put these pattern types behind a separate incomplete feature.

These are not necessary for the pattern types MVP (replacing the layout range attributes in libcore and rustc).

cc #136574 (new tracking issue for the generic_pattern_types feature gate)

r? @lcnr

cc @scottmcm @joshtriplett

@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2025

changes to the core type system

cc @compiler-errors, @lcnr

@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 5, 2025
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the pattern-types-generic branch from b025089 to f12b963 Compare February 5, 2025 15:06
Comment on lines 724 to 728
let c = self.tcx().normalize_erasing_regions(
self.infcx.typing_env(self.param_env),
c,
);
if c.has_aliases() || c.has_param() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit fishy, but I needed more than ClauseKind::ConstEvaluatable. ConstEvaluatable only checks that the constant itself will not fail to evaluate, which a generic parameter will also not (due to how we're enforcing things with const generics elsewhere).

So I took the knowledge that these constants always have concrete types and will normalize to error constants if they have errors themselves, to just evaluate here and check if there are still params or aliases around after normalization.

Oh and I need to normalize, can't just check for aliases and params, since we treat any unevaluated const as an alias, even if it refers to a free const. Which I guess makes sense as we want to cause normalization to actually get to the const and evaluate it.

Copy link
Member

Choose a reason for hiding this comment

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

I think you can/should just use c.has_param() and forget about even normalizing- min const generics doesn't permit Foo<{ <T as Trait>::USIZE }> where the assoc const can be normalized to usize regardless of T so I think design wise it's fine to not be "clever" here and just say that referring to generic parameters syntactically is forbidden.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why you need to check for aliases 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

uh yea, i think I just thoroughly confused myself. Changed it to avoid normalization and just checking for params. No params in the args of the unevaluated const is what we actually are interested in after all

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 5, 2025

r? @BoxyUwU unless you specifically want a review from lcnr? though my understanding was that they are not particularly involved with pattern types

@rustbot rustbot assigned BoxyUwU and unassigned lcnr Feb 5, 2025
@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 5, 2025
@oli-obk oli-obk force-pushed the pattern-types-generic branch from f12b963 to fab6d8a Compare February 6, 2025 09:53
@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 6, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 10, 2025

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 10, 2025

📌 Commit fab6d8a 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 10, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 10, 2025
…oxyUwU

Prevent generic pattern types from being used in libstd

Pattern types should follow the same rules that patterns follow. So a pattern type range must not wrap and not be empty. While we reject such invalid ranges at layout computation time, that only happens during monomorphization in the case of const generics. This is the exact same issue as other const generic math has, and since there's no solution there yet, I put these pattern types behind a separate incomplete feature.

These are not necessary for the pattern types MVP (replacing the layout range attributes in libcore and rustc).

cc rust-lang#136574 (new tracking issue for the `generic_pattern_types` feature gate)

r? `@lcnr`

cc `@scottmcm` `@joshtriplett`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 10, 2025
…oxyUwU

Prevent generic pattern types from being used in libstd

Pattern types should follow the same rules that patterns follow. So a pattern type range must not wrap and not be empty. While we reject such invalid ranges at layout computation time, that only happens during monomorphization in the case of const generics. This is the exact same issue as other const generic math has, and since there's no solution there yet, I put these pattern types behind a separate incomplete feature.

These are not necessary for the pattern types MVP (replacing the layout range attributes in libcore and rustc).

cc rust-lang#136574 (new tracking issue for the `generic_pattern_types` feature gate)

r? `@lcnr`

cc `@scottmcm` `@joshtriplett`
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 11, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#136107 (Introduce CoercePointeeWellformed for coherence checks at typeck stage)
 - rust-lang#136155 (Enable sanitizers on MSVC CI jobs)
 - rust-lang#136524 (Delay bug when method confirmation cannot upcast object pick of self)
 - rust-lang#136584 (Prevent generic pattern types from being used in libstd)
 - rust-lang#136603 (compiler: gate `extern "{abi}"` in ast_lowering)
 - rust-lang#136821 (assign marcoieni and jdno to infra-ci PRs)
 - rust-lang#136825 (Update books)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c92aae9 into rust-lang:master Feb 11, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Feb 11, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 11, 2025
Rollup merge of rust-lang#136584 - oli-obk:pattern-types-generic, r=BoxyUwU

Prevent generic pattern types from being used in libstd

Pattern types should follow the same rules that patterns follow. So a pattern type range must not wrap and not be empty. While we reject such invalid ranges at layout computation time, that only happens during monomorphization in the case of const generics. This is the exact same issue as other const generic math has, and since there's no solution there yet, I put these pattern types behind a separate incomplete feature.

These are not necessary for the pattern types MVP (replacing the layout range attributes in libcore and rustc).

cc rust-lang#136574 (new tracking issue for the `generic_pattern_types` feature gate)

r? ``@lcnr``

cc ``@scottmcm`` ``@joshtriplett``
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.

7 participants