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

Poor error message for type definitions in extern blocks #43495

Closed
jimblandy opened this issue Jul 26, 2017 · 3 comments
Closed

Poor error message for type definitions in extern blocks #43495

jimblandy opened this issue Jul 26, 2017 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jimblandy
Copy link
Contributor

Although work on RFC 1861 for extern types has begun in issue #43467, it's not complete, and in the mean time, Rust gives very strange messages if one accidentally includes a type in an extern block. For example:

use std::ffi::CString;
use std::os::raw::c_char;

extern {
    fn strlen(s: *const c_char) -> usize;
    pub struct S(c_char); 
}

fn main() {
    println!("{}", unsafe { strlen(CString::new("foo").unwrap().as_ptr()) });
}

On nightly, this elicits the following message:

   Compiling playground v0.0.1 (file:///playground)
error: unmatched visibility `pub`
 --> src/main.rs:7:5
  |
7 |     pub struct S(c_char); 
  |     ^^^

A reader of our book came across this error message by accident and asked me about it. We were both mystified.

@jimblandy
Copy link
Contributor Author

Possibly related to issue #40599.

Perhaps this is deliberate, as the parse-fail/duplicate-visibility.rs test expects it. But I still think it's unclear.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@steveklabnik
Copy link
Member

Triage: this error message got weirder:

error: expected one of `(`, `fn`, `static`, or `type`, found keyword `struct`
 --> src/main.rs:6:9
  |
6 |     pub struct S(c_char); 
  |         ^^^^^^ expected one of `(`, `fn`, `static`, or `type` here

@steveklabnik steveklabnik 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. labels Dec 25, 2019
@Centril
Copy link
Contributor

Centril commented Mar 26, 2020

We now recognize this as a struct and emit a good error:

error: struct is not supported in `extern` blocks
 --> src/main.rs:6:5
  |
6 |     pub struct S(c_char); 
  |     ^^^^^^^^^^^^^^^^^^^^^

@Centril Centril closed this as completed Mar 26, 2020
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants