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 error message when global_asm! uses asm! operands #128305

Merged
merged 1 commit into from
Aug 4, 2024

Conversation

folkertdev
Copy link
Contributor

follow-up to #128207

what was

error: expected expression, found keyword `in`
 --> src/lib.rs:1:31
  |
1 | core::arch::global_asm!("{}", in(reg));
  |                               ^^ expected expression

becomes

error: the `in` operand cannot be used with `global_asm!`
  --> $DIR/parse-error.rs:150:19
   |
LL | global_asm!("{}", in(reg));
   |                   ^^ the `in` operand is not meaningful for global-scoped inline assembly, remove it

the span of the error is just the keyword, which means that we can't create a machine-applicable suggestion here. The alternative would be to attempt to parse the full operand, but then if there are syntax errors in the operand those would be presented to the user, even though the parser already knows that the output won't be valid. Also that would require more complexity in the parser.

So I think this is a nice improvement at very low cost.

@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2024

r? @fmease

rustbot has assigned @fmease.
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 28, 2024
Comment on lines +202 to +203
builtin_macros_global_asm_unsupported_operand = the `{$symbol}` operand cannot be used with `global_asm!`
.label = the `{$symbol}` operand is not meaningful for global-scoped inline assembly, remove it
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on https://doc.rust-lang.org/reference/inline-assembly.html#syntax, it seems like in/out/inout/etc are "direction specifiers", while "operand" includes the variable. So maybe "specifier" rather than "operand" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the word "operand" is used for the whole thing, so all of x = inout(reg) x, is an operand. The word "specifier" does not occur on that page, "spec" only in the grammar.

The section describing these items is https://doc.rust-lang.org/reference/inline-assembly.html#operand-type. It says that "Several types of operands are supported" and then lists in, out etc. So I think using the word "operand" is the most helpful.

The error message points to the specifier specifically because it is easy to parse without emitting any unhelpful errors, but the operand as a whole is unsupported.

Comment on lines +32 to +40
/// Used for better error messages when operand types are used that are not
/// supported by the current macro (e.g. `in` or `out` for `global_asm!`)
///
/// returns
///
/// - `Ok(true)` if the current token matches the keyword, and was expected
/// - `Ok(false)` if the current token does not match the keyword
/// - `Err(_)` if the current token matches the keyword, but was not expected
fn eat_operand_keyword<'a>(p: &mut Parser<'a>, symbol: Symbol, expect: bool) -> PResult<'a, bool> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Every time this gets called, expect is !is_global_asm. Maybe change expect to disallowed and reverse that logic, so you only need to pass is_global_asm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

like the previous PR, this is in preparation of adding naked_asm!. That will pass an enum as the third argument to decide whether the symbol is expected. For now I think going with the concept of "expected" makes sense because that is the terminology that the parser uses.

Copy link
Contributor Author

@folkertdev folkertdev left a comment

Choose a reason for hiding this comment

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

r? @Amanieu for consistency with #128207

Comment on lines +202 to +203
builtin_macros_global_asm_unsupported_operand = the `{$symbol}` operand cannot be used with `global_asm!`
.label = the `{$symbol}` operand is not meaningful for global-scoped inline assembly, remove it
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the word "operand" is used for the whole thing, so all of x = inout(reg) x, is an operand. The word "specifier" does not occur on that page, "spec" only in the grammar.

The section describing these items is https://doc.rust-lang.org/reference/inline-assembly.html#operand-type. It says that "Several types of operands are supported" and then lists in, out etc. So I think using the word "operand" is the most helpful.

The error message points to the specifier specifically because it is easy to parse without emitting any unhelpful errors, but the operand as a whole is unsupported.

Comment on lines +32 to +40
/// Used for better error messages when operand types are used that are not
/// supported by the current macro (e.g. `in` or `out` for `global_asm!`)
///
/// returns
///
/// - `Ok(true)` if the current token matches the keyword, and was expected
/// - `Ok(false)` if the current token does not match the keyword
/// - `Err(_)` if the current token matches the keyword, but was not expected
fn eat_operand_keyword<'a>(p: &mut Parser<'a>, symbol: Symbol, expect: bool) -> PResult<'a, bool> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

like the previous PR, this is in preparation of adding naked_asm!. That will pass an enum as the third argument to decide whether the symbol is expected. For now I think going with the concept of "expected" makes sense because that is the terminology that the parser uses.

@rustbot rustbot assigned Amanieu and unassigned fmease Jul 29, 2024
@Amanieu
Copy link
Member

Amanieu commented Aug 4, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Aug 4, 2024

📌 Commit 571f7b6 has been approved by Amanieu

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 4, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 4, 2024
…operand, r=Amanieu

improve error message when `global_asm!` uses `asm!` operands

follow-up to rust-lang#128207

what was

```
error: expected expression, found keyword `in`
 --> src/lib.rs:1:31
  |
1 | core::arch::global_asm!("{}", in(reg));
  |                               ^^ expected expression
```

becomes

```
error: the `in` operand cannot be used with `global_asm!`
  --> $DIR/parse-error.rs:150:19
   |
LL | global_asm!("{}", in(reg));
   |                   ^^ the `in` operand is not meaningful for global-scoped inline assembly, remove it
```

the span of the error is just the keyword, which means that we can't create a machine-applicable suggestion here. The alternative would be to attempt to parse the full operand, but then if there are syntax errors in the operand those would  be presented to the user, even though the parser already knows that the output won't be valid. Also that would require more complexity in the parser.

So I think this is a nice improvement at very low cost.
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#128305 (improve error message when `global_asm!` uses `asm!` operands)
 - rust-lang#128362 (add test for symbol visibility of `#[naked]` functions)
 - rust-lang#128526 (time.rs: remove "Basic usage text")
 - rust-lang#128531 (Miri: add a flag to do recursive validity checking)
 - rust-lang#128589 (allow setting `link-shared` and `static-libstdcpp` with CI LLVM)
 - rust-lang#128615 (rustdoc: make the hover trail for doc anchors a bit bigger)
 - rust-lang#128620 (Update rinja version to 0.3.0)

r? `@ghost`
`@rustbot` modify labels: rollup
@matthiaskrgr
Copy link
Member

@bors r-
#128624 (comment)

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 4, 2024
@folkertdev
Copy link
Contributor Author

@matthiaskrgr I believe the reported error is actually with #128362, not with this PR. I think this PR is actually fine

@matthiaskrgr
Copy link
Member

Mh right the file seems to come from the other PR? oops :D

I'll wait to see if the other rollup fails as well..

@matthiaskrgr
Copy link
Member

right, rollup still failed ✨
@bors r=Amanieu

@bors
Copy link
Contributor

bors commented Aug 4, 2024

📌 Commit 571f7b6 has been approved by Amanieu

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 4, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#128305 (improve error message when `global_asm!` uses `asm!` operands)
 - rust-lang#128526 (time.rs: remove "Basic usage text")
 - rust-lang#128531 (Miri: add a flag to do recursive validity checking)
 - rust-lang#128578 (rustdoc: Cleanup `CacheBuilder` code for building search index)
 - rust-lang#128589 (allow setting `link-shared` and `static-libstdcpp` with CI LLVM)
 - rust-lang#128615 (rustdoc: make the hover trail for doc anchors a bit bigger)
 - rust-lang#128620 (Update rinja version to 0.3.0)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b6b8330 into rust-lang:master Aug 4, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2024
Rollup merge of rust-lang#128305 - folkertdev:asm-parser-unsupported-operand, r=Amanieu

improve error message when `global_asm!` uses `asm!` operands

follow-up to rust-lang#128207

what was

```
error: expected expression, found keyword `in`
 --> src/lib.rs:1:31
  |
1 | core::arch::global_asm!("{}", in(reg));
  |                               ^^ expected expression
```

becomes

```
error: the `in` operand cannot be used with `global_asm!`
  --> $DIR/parse-error.rs:150:19
   |
LL | global_asm!("{}", in(reg));
   |                   ^^ the `in` operand is not meaningful for global-scoped inline assembly, remove it
```

the span of the error is just the keyword, which means that we can't create a machine-applicable suggestion here. The alternative would be to attempt to parse the full operand, but then if there are syntax errors in the operand those would  be presented to the user, even though the parser already knows that the output won't be valid. Also that would require more complexity in the parser.

So I think this is a nice improvement at very low cost.
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.

7 participants