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

Add lint for recursive default impls #128737

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

Conversation

mj10021
Copy link
Contributor

@mj10021 mj10021 commented Aug 6, 2024

Fixes #128421, where it was pointed out that recursion in default impls can be confusing and always results in failure.

@rustbot
Copy link
Collaborator

rustbot commented Aug 6, 2024

r? @chenyukang

rustbot has assigned @chenyukang.
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 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 Aug 6, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

I don't think this should be a new lint. We already have a lint for when we detect recursive calls, and we can change the wording for that lint if we detect that it's a recursive Default invocation.

/// default struct for this field. This will always lead to an infinite loop
/// so it is denied.
pub RECURSIVE_DEFAULT_IMPL,
Deny,
Copy link
Member

Choose a reason for hiding this comment

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

This needs T-lang approval if it's going to be a Deny lint.

@mj10021
Copy link
Contributor Author

mj10021 commented Aug 6, 2024

I don't think this should be a new lint. We already have a lint for when we detect recursive calls, and we can change the wording for that lint if we detect that it's a recursive Default invocation.

Do you think that it makes sense for the Self { ..Default::default() } case to be denied since it is a guaranteed infinite loop or it is too specific off a case to warrant special treatment?

@mj10021
Copy link
Contributor Author

mj10021 commented Aug 6, 2024

@rustbot label +T-Lang

@rustbot rustbot added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Aug 6, 2024
@chenyukang
Copy link
Member

yeah, I think it's better to change the wording for existing lint for this specific scenario.

@chenyukang
Copy link
Member

@rustbot author

@rustbot rustbot 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 Aug 24, 2024
@compiler-errors compiler-errors self-assigned this Aug 24, 2024
@bors
Copy link
Contributor

bors commented Sep 23, 2024

☔ The latest upstream changes (presumably #130724) made this pull request unmergeable. Please resolve the merge conflicts.

@alex-semenyuk
Copy link
Member

@mj10021
From wg-triage. Do you have update on this PR?

@mj10021
Copy link
Contributor Author

mj10021 commented Oct 31, 2024

@mj10021 From wg-triage. Do you have update on this PR?

Oops, this got away from me, will update the PR shortly

@mj10021
Copy link
Contributor Author

mj10021 commented Nov 5, 2024

Does this look more like the type of note that was in mind?

@Dylan-DPC Dylan-DPC 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 Nov 6, 2024
compiler/rustc_mir_build/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/errors.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/lints.rs Outdated Show resolved Hide resolved
@rustbot rustbot 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 Nov 21, 2024
@@ -265,6 +265,8 @@ mir_build_pointer_pattern = function pointers and raw pointers not derived from
mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future
mir_build_recursive_default_impl = calling ..Default::default() in a Default implementation leads to infinite recursion, and cannotbe used to initialize the fields of a struct or enum
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
mir_build_recursive_default_impl = calling ..Default::default() in a Default implementation leads to infinite recursion, and cannotbe used to initialize the fields of a struct or enum
mir_build_recursive_default_impl = calling `..Default::default()` in a `Default` implementation leads to infinite recursion, and does not initialize the fields of a struct or enum

Copy link
Member

Choose a reason for hiding this comment

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

why did you not fix the backticks?

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 my mistake sorry

Comment on lines 34 to 35
use rustc_span::def_id::LocalDefId;
use rustc_span::sym;
Copy link
Member

Choose a reason for hiding this comment

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

put these imports at the top of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recursive ..Default::default() overflows
8 participants