Skip to content

Fortify generic param default checks #144977

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

Merged
merged 1 commit into from
Aug 7, 2025

Conversation

fmease
Copy link
Member

@fmease fmease commented Aug 5, 2025

  • Hard-reject instead of lint-reject type param defaults in generic assoc consts (GACs) (feature: generic_const_items).
    • In Implement generic const items #113522, I explicitly handled the free const item case and forgot about the assoc const one.
    • This led rustc to assume the default of emitting the deny-by-default lint invalid_type_param_default.
    • GCIs are unstable, thus we're not bound by backward compat
  • Hard-reject instead of lint-reject type param defaults in foreign items.
    • We already hard-reject generic params on foreign items, so this isn't a breaking change.
    • There's no reason why we need to lint-reject.
  • Refactor the way we determine where generic param defaults are allowed:
    • Don't default to emitting lint invalid_type_param_defaults for nodes that aren't explicitly handled but instead panic.
    • This would've caught my GAC oversight from above much earlier via fuzzing
    • Prevents us from accidentally stabilizing more invalid type param defaults in the future
  • Streamline the phrasing of the diagnostic

@rustbot
Copy link
Collaborator

rustbot commented Aug 5, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-tidy Area: The tidy tool S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 5, 2025
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

ItemKind::Fn { .. } | ItemKind::Impl(_) => ParamDefaultPolicy::FutureCompatForbidden,
// Re. GCI, we're not bound by backward compatibility.
ItemKind::Const(..) => ParamDefaultPolicy::Forbidden,
_ => return None,
Copy link
Member Author

Choose a reason for hiding this comment

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

I could exhaustively match on item.kind if you want me to.

// compatibility, let's hard-reject defaults on them.
Node::ForeignItem(_) => ParamDefaultPolicy::Forbidden,
Node::OpaqueTy(..) => ParamDefaultPolicy::Allowed,
_ => return None,
Copy link
Member Author

Choose a reason for hiding this comment

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

Exhaustively matching on node: hir::Node doesn't really buy us much and it would be pretty overkill.

Copy link
Member Author

@fmease fmease Aug 5, 2025

Choose a reason for hiding this comment

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

I've combined tests/ui/type/default_type_parameter_in_fn_or_impl.rs and tests/ui/issues/issue-26812.rs into this file.

@fmease fmease force-pushed the fortify-param-default-checks branch from b51eb86 to 02ea38c Compare August 5, 2025 23:30
@fmease fmease removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-tidy Area: The tidy tool labels Aug 5, 2025
}
GenericParamKind::Const { ty: _, default, synthetic } => {
if default.is_some() {
match param_default_policy.expect("no policy for generic param default") {
Copy link
Member Author

@fmease fmease Aug 6, 2025

Choose a reason for hiding this comment

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

Of course, instead of panicking we could simply default to Forbidden (on master we default to FutureCompatForbidden which I heavily dislike). Just LMK :)

@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 6, 2025

📌 Commit 02ea38c has been approved by compiler-errors

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 Aug 6, 2025
bors added a commit that referenced this pull request Aug 6, 2025
Rollup of 15 pull requests

Successful merges:

 - #144195 (Parser: Recover from attributes applied to types and generic args)
 - #144794 (Port `#[coroutine]` to the new attribute system)
 - #144835 (Anonymize binders in tail call sig)
 - #144861 (Stabilize `panic_payload_as_str` feature)
 - #144917 (Enforce tail call type is related to body return type in borrowck)
 - #144948 (we only merge candidates for trait and normalizes-to goals)
 - #144956 (Gate const trait syntax)
 - #144970 (rustdoc: fix caching of intra-doc links on reexports)
 - #144972 (add code example showing that file_prefix treats dotfiles as the name of a file, not an extension)
 - #144975 (`File::set_times`: Update documentation and example to support setting timestamps on directories)
 - #144977 (Fortify generic param default checks)
 - #144996 (simplifycfg: Mark as changed when start is modified in collapse goto chain)
 - #144998 (mir: Do not modify NonUse in `super_projection_elem`)
 - #145000 (Remove unneeded `stage` parameter when setting up stdlib Cargo)
 - #145008 (Fix rustdoc scrape examples crash)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 12d1b17 into rust-lang:master Aug 7, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 7, 2025
rust-timer added a commit that referenced this pull request Aug 7, 2025
Rollup merge of #144977 - fmease:fortify-param-default-checks, r=compiler-errors

Fortify generic param default checks

* Hard-reject instead of lint-reject type param defaults in generic assoc consts (GACs) (feature: `generic_const_items`).
  * In #113522, I explicitly handled the free const item case and forgot about the assoc const one.
  * This led rustc to assume the default of emitting the deny-by-default lint `invalid_type_param_default`.
  * GCIs are unstable, thus we're not bound by backward compat
* Hard-reject instead of lint-reject type param defaults in foreign items.
  * We already hard-reject generic params on foreign items, so this isn't a breaking change.
  * There's no reason why we need to lint-reject.
* Refactor the way we determine where generic param defaults are allowed:
  * Don't default to emitting lint `invalid_type_param_defaults` for nodes that aren't explicitly handled but instead panic.
  * This would've caught my GAC oversight from above much earlier via fuzzing
  * Prevents us from accidentally stabilizing more invalid type param defaults in the future
* Streamline the phrasing of the diagnostic
@fmease fmease deleted the fortify-param-default-checks branch August 7, 2025 03:16
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