Skip to content

Clarify E0381 diagnostics for branch conditions#156603

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
eval-exec:e0381-diagnostic-condition-note
Jun 2, 2026
Merged

Clarify E0381 diagnostics for branch conditions#156603
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
eval-exec:e0381-diagnostic-condition-note

Conversation

@eval-exec
Copy link
Copy Markdown
Contributor

@eval-exec eval-exec commented May 15, 2026

this pr want to fix: #156494

Copilot AI review requested due to automatic review settings May 15, 2026 11:09
@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 May 15, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 15, 2026

r? @mati865

rustbot has assigned @mati865.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

@eval-exec eval-exec marked this pull request as draft May 15, 2026 11:12
@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 May 15, 2026

This comment was marked as off-topic.

@rust-log-analyzer

This comment has been minimized.

@eval-exec eval-exec force-pushed the e0381-diagnostic-condition-note branch from b0e5f62 to cb680f9 Compare May 15, 2026 11:33
@eval-exec eval-exec marked this pull request as ready for review May 15, 2026 11:34
@rustbot rustbot 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 May 15, 2026
@eval-exec eval-exec marked this pull request as draft May 15, 2026 11:34
@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 May 15, 2026
@eval-exec eval-exec marked this pull request as ready for review May 15, 2026 11:36
@rustbot rustbot 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 May 15, 2026
@mati865
Copy link
Copy Markdown
Member

mati865 commented May 15, 2026

@rustbot reroll

@rustbot rustbot assigned nikomatsakis and unassigned mati865 May 15, 2026
Copy link
Copy Markdown
Member

@hkBst hkBst left a comment

Choose a reason for hiding this comment

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

I'm not convinced the ConditionKind::Other variant makes sense. I think either we unconditionally give the disclaimer about the compiler not considering values for its reasoning, or we rephrase its messages to talk about then-branches and else-branches, without mentioning whether the if-condition might be true or false. That way we don't imply that the compiler cares about the value of the condition, but only that a certain branch lacks initialization.

View changes since this review

Comment on lines +4714 to +4715
fn new(span: Span, kind: ConditionErrorKind, label: String) -> Self {
Self { span, label, kind }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Field ordering is not consistent between new and struct definition. Unless there is a good reason for that, I think it would be less surprising if the order were consistent.

Comment on lines +4839 to 4842
ConditionErrorKind::Other,
format!(
"if this pattern is matched, {} is not initialized",
self.name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could not a pattern match lead to the same kind of diagnostic? If a variable is initialized as None, and I match on Some(_) and then don't initialize something in that branch, then we are in the same situation I think.

Comment on lines +4808 to 4811
ConditionErrorKind::Other,
format!(
"if the `for` loop runs 0 times, {} is not initialized",
self.name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about the case where the for-loop loops i times and let i = 1;? Are we then not also in the situation where the compiler does not consider a value for its diagnostic?

Comment on lines +4748 to 4750
ConditionErrorKind::Other,
format!("an `else` arm might be missing here, initializing {}", self.name),
));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The else-branch might be unreachable because of the values of variables in the program, which would mean the same kind of disclaimer would apply to this case as well.

Copy link
Copy Markdown
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Nice improvement!

View changes since this review

@nikomatsakis
Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 2, 2026

📌 Commit cb680f9 has been approved by nikomatsakis

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

@rust-bors rust-bors Bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jun 2, 2026
@rust-bors rust-bors Bot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 2, 2026
@hkBst
Copy link
Copy Markdown
Member

hkBst commented Jun 2, 2026

@nikomatsakis Did you have an opinion about my review comments? I believe this is making a distinction that is not really there, as shown by the examples in said comments.

rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #155418 (transmute: fix check for whether newtypes have equal size)
 - #156603 (Clarify E0381 diagnostics for branch conditions)
 - #156643 (Document run-make external dependencies)
 - #157009 (Avoid `unreachable_code` on required return values)
 - #157308 (make typing mode exhaustive again...)
 - #157312 (disallow most attrs on eiis)
rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #155418 (transmute: fix check for whether newtypes have equal size)
 - #156603 (Clarify E0381 diagnostics for branch conditions)
 - #156643 (Document run-make external dependencies)
 - #157009 (Avoid `unreachable_code` on required return values)
 - #157308 (make typing mode exhaustive again...)
 - #157312 (disallow most attrs on eiis)
@rust-bors rust-bors Bot merged commit b1e7469 into rust-lang:main Jun 2, 2026
11 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 2, 2026
rust-timer added a commit that referenced this pull request Jun 2, 2026
Rollup merge of #156603 - eval-exec:e0381-diagnostic-condition-note, r=nikomatsakis

Clarify E0381 diagnostics for branch conditions

this pr want to fix: #156494
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.

E0381 incorrectly reports potentially uninitialized usage

7 participants