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

compiler: suggest const _ for a misplaced const {} #128374

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

workingjubilee
Copy link
Member

Fixes #128338

@rustbot
Copy link
Collaborator

rustbot commented Jul 29, 2024

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
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 Jul 29, 2024
@@ -81,6 +81,11 @@ impl<'a> Parser<'a> {
span,
"consider using `const` or `static` instead of `let` for global variables",
);
} else if self.parse_const_block(span, false).is_ok() {
Copy link
Member

Choose a reason for hiding this comment

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

Using is_ok here is kinda hazardous since you need to cancel the diagnostic on the bad path.

Copy link
Member Author

Choose a reason for hiding this comment

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

that is... definitely the part I was least certain about, yes. is there, like, a sample of Rust that will now elicit some disastrously bad diagnostic as a result of doing this, or...?

Copy link
Member

Choose a reason for hiding this comment

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

No, if you drop a Diagnostic without emitting it the compiler will ICE.

Presumably you will have some code that looks like:

match self.parse_const_block(span, false) {
  Err(e) => { e.cancel(); }
  Ok(_) => { do the recovery code here }
}

Which at that point we probably should do snapshotting too, so try this:

let snapshot = self.create_snapshot_for_diagnostic();
match self.parse_const_block(span, false) {
  Err(e) => { 
       e.cancel();
       self.recover_from_snapshot(snapshot);
   }
  Ok(_) => { do the recovery code here }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

No, if you drop a Diagnostic without emitting it the compiler will ICE.

...oh lmao, drop bombs away.

Copy link
Member

Choose a reason for hiding this comment

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

Example of the drop bomb in question -- this ICEs after your PR:

const { a: () = 1; };

@@ -81,6 +81,11 @@ impl<'a> Parser<'a> {
span,
"consider using `const` or `static` instead of `let` for global variables",
);
} else if self.parse_const_block(span, false).is_ok() {
Copy link
Member

Choose a reason for hiding this comment

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

This logic should probably be moved to parse_item (well, one of its helpers like parse_item_common) since this is really an item recovery.

Copy link
Member Author

Choose a reason for hiding this comment

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

should I just yeet all these suggestions that are secretly item recoveries over there?

Copy link
Member

Choose a reason for hiding this comment

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

If you'd like to do so, please do.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I think it's important that both of these suggestions continue to live next to each other so they get refactored together.

Comment on lines 85 to 90
err.span_label(
span,
Copy link
Member

Choose a reason for hiding this comment

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

Also please use a structured suggestion (with Applicability::MaybeIncorrect)

err.span_suggestion_verbose(
    span.shrink_to_lo(),
    /* ... */
)

Copy link
Member Author

Choose a reason for hiding this comment

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

The suggestion above this one for the kw::Let case should also be using something like that, right?

Copy link
Member

Choose a reason for hiding this comment

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

Yea, though that one's harder because it has two choices. You can try to use span_suggestions if you'd like.

@rust-log-analyzer

This comment has been minimized.

@workingjubilee
Copy link
Member Author

oh THERE'S the broken tests lmao

@compiler-errors
Copy link
Member

@workingjubilee:

You should probably also check that the current token is kw::Const before proceeding to call parse_const. You may also want to check that the self.lookahead(1, ..)'th token is {.

@workingjubilee
Copy link
Member Author

fun times ahead,

@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 Jul 30, 2024
@workingjubilee workingjubilee marked this pull request as draft September 19, 2024 23:50
@rust-log-analyzer

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@workingjubilee
Copy link
Member Author

genuinely not sure what is going on there with that parse error going away....

@workingjubilee
Copy link
Member Author

probably because these recoveries are in the wrong place, as discussed.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
------
 > importing cache manifest from ghcr.io/rust-lang/rust-ci-cache:30ca74372d8b771363f68f939a58b017a592fae4f69398600dc51145997160f03e9652051f957840c41898984a88855e9757fa23464703a5a4ba21316ddebb04:
------
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---

9    = help: pass `--edition 2021` to `rustc`
10    = note: for more on editions, read https://doc.rust-lang.org/edition-guide
11 
- error: expected item, found `"owned_box"`
-    |
-    |
- LL |     s#[c"owned_box"]
-    |
-    = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
- 
- error: aborting due to 2 previous errors
---
To only update this specific test, also pass `--test-args parser/help-set-edition-ice-122130.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/parser/help-set-edition-ice-122130.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/parser/help-set-edition-ice-122130" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/parser/help-set-edition-ice-122130/auxiliary"
--- stderr -------------------------------
--- stderr -------------------------------
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `#`
   |
   |
LL |     s#[c"owned_box"]
   |      ^ expected one of `(`, `,`, `=`, `{`, or `}`
   = note: you may be trying to write a c-string literal
   = note: you may be trying to write a c-string literal
   = note: c-string literals require Rust 2021 or later
   = help: pass `--edition 2021` to `rustc`
   = note: for more on editions, read https://doc.rust-lang.org/edition-guide
error: aborting due to 1 previous error
------------------------------------------


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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

const { ... } blocks cannot be written outside of function
6 participants