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

Suboptimal error message for new extern block syntax #126327

Closed
GuillaumeGomez opened this issue Jun 12, 2024 · 1 comment · Fixed by #127106
Closed

Suboptimal error message for new extern block syntax #126327

GuillaumeGomez opened this issue Jun 12, 2024 · 1 comment · Fixed by #127106
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GuillaumeGomez
Copy link
Member

With the following code:

extern {
    pub fn foo();
    pub safe fn bar();
}

We get this error:

error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> foo.rs:4:5
  |
1 | extern {
  | ------ help: add unsafe to this `extern` block
...
4 |     pub safe fn bar();
  |     ^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Making the extern block unsafe then gives:

error: extern block cannot be declared unsafe
 --> foo.rs:1:1
  |
1 | unsafe extern {
  | ^^^^^^

error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> foo.rs:4:5
  |
1 | unsafe extern {
  | ------------- help: add unsafe to this `extern` block
...
4 |     pub safe fn bar();
  |     ^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

cc @spastorino

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 12, 2024
@spastorino
Copy link
Member

👍, this should error in both cases but the diagnostics are misleading. Going to fix it.

r? @spastorino

@GuillaumeGomez GuillaumeGomez added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 12, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 29, 2024
…locks-diagnostics, r=compiler-errors

Improve unsafe extern blocks diagnostics

Closes rust-lang#126327

For this code:

```rust
extern {
    pub fn foo();
    pub safe fn bar();
}
```

We get ...

```
error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> test.rs:3:5
  |
3 |     pub safe fn bar();
  |     ^^^^^^^^^^^^^^^^^^
  |
help: add unsafe to this `extern` block
  |
1 | unsafe extern {
  | ++++++

error[E0658]: `unsafe extern {}` blocks and `safe` keyword are experimental
 --> test.rs:3:9
  |
3 |     pub safe fn bar();
  |         ^^^^
  |
  = note: see issue rust-lang#123743 <rust-lang#123743> for more information
  = help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
```

And then making the extern block unsafe, we get ...

```
error: extern block cannot be declared unsafe
 --> test.rs:1:1
  |
1 | unsafe extern {
  | ^^^^^^
  |
  = note: see issue rust-lang#123743 <rust-lang#123743> for more information
  = help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable

error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> test.rs:3:5
  |
3 |     pub safe fn bar();
  |     ^^^^^^^^^^^^^^^^^^

error[E0658]: `unsafe extern {}` blocks and `safe` keyword are experimental
 --> test.rs:3:9
  |
3 |     pub safe fn bar();
  |         ^^^^
  |
  = note: see issue rust-lang#123743 <rust-lang#123743> for more information
  = help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
```

r? `@compiler-errors`
@bors bors closed this as completed in 7715295 Jun 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 30, 2024
Rollup merge of rust-lang#127106 - spastorino:improve-unsafe-extern-blocks-diagnostics, r=compiler-errors

Improve unsafe extern blocks diagnostics

Closes rust-lang#126327

For this code:

```rust
extern {
    pub fn foo();
    pub safe fn bar();
}
```

We get ...

```
error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> test.rs:3:5
  |
3 |     pub safe fn bar();
  |     ^^^^^^^^^^^^^^^^^^
  |
help: add unsafe to this `extern` block
  |
1 | unsafe extern {
  | ++++++

error[E0658]: `unsafe extern {}` blocks and `safe` keyword are experimental
 --> test.rs:3:9
  |
3 |     pub safe fn bar();
  |         ^^^^
  |
  = note: see issue rust-lang#123743 <rust-lang#123743> for more information
  = help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
```

And then making the extern block unsafe, we get ...

```
error: extern block cannot be declared unsafe
 --> test.rs:1:1
  |
1 | unsafe extern {
  | ^^^^^^
  |
  = note: see issue rust-lang#123743 <rust-lang#123743> for more information
  = help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable

error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> test.rs:3:5
  |
3 |     pub safe fn bar();
  |     ^^^^^^^^^^^^^^^^^^

error[E0658]: `unsafe extern {}` blocks and `safe` keyword are experimental
 --> test.rs:3:9
  |
3 |     pub safe fn bar();
  |         ^^^^
  |
  = note: see issue rust-lang#123743 <rust-lang#123743> for more information
  = help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
```

r? ``@compiler-errors``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants