Skip to content

Poor error message for type definitions in extern blocks #43495

Closed
@jimblandy

Description

@jimblandy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions