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

Improve invalid let expression handling #115677

Merged
merged 5 commits into from Sep 14, 2023

Conversation

matthewjasper
Copy link
Contributor

@matthewjasper matthewjasper commented Sep 8, 2023

  • Move all of the checks for valid let expression positions to parsing.
  • Add a field to ExprKind::Let in AST/HIR to mark whether it's in a valid location.
  • Suppress some later errors and MIR construction for invalid let expressions.
  • Fix a (drop) scope issue that was also responsible for ICE "region scope ... not in scope to drop" #104172.

Fixes #104172
Fixes #104868

@rustbot
Copy link
Collaborator

rustbot commented Sep 8, 2023

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@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 Sep 8, 2023
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Sep 8, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rust-log-analyzer

This comment has been minimized.

These scopes would not exist in MIR and can cause ICEs with invalid uses
of let expressions.
There was an incomplete version of the check in parsing and a second
version in AST validation. This meant that some, but not all, invalid
uses were allowed inside macros/disabled cfgs. It also means that later
passes have a hard time knowing when the let expression is in a valid
location, sometimes causing ICEs.

- Add a field to ExprKind::Let in AST/HIR to mark whether it's in a
  valid location.
- Suppress later errors and MIR construction for invalid let
  expressions.
Previously some invalid let expressions would result in both a feature
error and a parsing error. Avoid this and ensure that we only emit the
parsing error when this happens.
} else {
self.parse_expr_closure()
this.parse_expr_lit()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this change necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The diff isn't showing the changes here very well (maybe due to the extra indent). There isn't actually a change in the methods being called here.

#[derive(Clone, Copy, Subdiagnostic)]
pub(crate) enum ForbiddenLetReason {
/// `let` is not valid and the source environment is not important
GenericForbidden,
Copy link
Contributor

Choose a reason for hiding this comment

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

The generic in here is slightly confusing, can we maybe use another name here?

NotSupportedParentheses(#[primary_span] Span),
}

struct CondChecker<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment for this struct would be helpful, I think.

--> $DIR/bad-if-let-suggestion.rs:5:8
|
LL | if let x = 1 && i = 2 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
Copy link
Contributor

Choose a reason for hiding this comment

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

Not having this note is a slight regression imo.

- Add doc comment to new type
- Restore "only supported directly in conditions of `if` and `while` expressions" note
- Rename variant with clearer name
@b-naber
Copy link
Contributor

b-naber commented Sep 13, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Sep 13, 2023

📌 Commit e324a59 has been approved by b-naber

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 Sep 13, 2023
@bors
Copy link
Contributor

bors commented Sep 14, 2023

⌛ Testing commit e324a59 with merge 9d21092...

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 14, 2023
…-naber

Improve invalid let expression handling

- Move all of the checks for valid let expression positions to parsing.
- Add a field to ExprKind::Let in AST/HIR to mark whether it's in a valid location.
- Suppress some later errors and MIR construction for invalid let expressions.
- Fix a (drop) scope issue that was also responsible for rust-lang#104172.

Fixes rust-lang#104172
Fixes rust-lang#104868
@bors
Copy link
Contributor

bors commented Sep 14, 2023

☀️ Test successful - checks-actions
Approved by: b-naber
Pushing 9d21092 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 14, 2023
@bors
Copy link
Contributor

bors commented Sep 14, 2023

👀 Test was successful, but fast-forwarding failed: 422 Update is not a fast forward

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9d21092): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.6% [0.6%, 0.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [0.6%, 0.6%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 631.556s -> 632.444s (0.14%)
Artifact size: 317.95 MiB -> 317.98 MiB (0.01%)

@b-naber
Copy link
Contributor

b-naber commented Sep 14, 2023

@bors retry

@bors
Copy link
Contributor

bors commented Sep 14, 2023

⌛ Testing commit e324a59 with merge dac91a8...

@bors
Copy link
Contributor

bors commented Sep 14, 2023

☀️ Test successful - checks-actions
Approved by: b-naber
Pushing dac91a8 to master...

@bors bors merged commit dac91a8 into rust-lang:master Sep 14, 2023
12 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 14, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (dac91a8): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.2%, 0.3%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
4.2% [4.2%, 4.2%] 1
Regressions ❌
(secondary)
1.0% [0.7%, 1.3%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.2% [4.2%, 4.2%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 631.307s -> 631.195s (-0.02%)
Artifact size: 317.98 MiB -> 318.01 MiB (0.01%)

@matthewjasper matthewjasper deleted the let-expr-recovery branch September 18, 2023 08:30
flip1995 pushed a commit to flip1995/rust that referenced this pull request Sep 25, 2023
…-naber

Improve invalid let expression handling

- Move all of the checks for valid let expression positions to parsing.
- Add a field to ExprKind::Let in AST/HIR to mark whether it's in a valid location.
- Suppress some later errors and MIR construction for invalid let expressions.
- Fix a (drop) scope issue that was also responsible for rust-lang#104172.

Fixes rust-lang#104172
Fixes rust-lang#104868
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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
6 participants