Skip to content

Commit

Permalink
Make #[repr(Rust)] and #[repr(C)] incompatible with one another
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Oct 17, 2023
1 parent 1bc0463 commit cf1fba5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
13 changes: 10 additions & 3 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,7 @@ impl CheckAttrVisitor<'_> {
.collect();

let mut int_reprs = 0;
let mut is_explicit_rust = false;
let mut is_c = false;
let mut is_simd = false;
let mut is_transparent = false;
Expand All @@ -1778,7 +1779,9 @@ impl CheckAttrVisitor<'_> {
}

match hint.name_or_empty() {
sym::Rust => {}
sym::Rust => {
is_explicit_rust = true;
}
sym::C => {
is_c = true;
match target {
Expand Down Expand Up @@ -1888,12 +1891,16 @@ impl CheckAttrVisitor<'_> {

// Error on repr(transparent, <anything else>).
if is_transparent && hints.len() > 1 {
let hint_spans: Vec<_> = hint_spans.clone().collect();
let hint_spans = hint_spans.clone().collect();
self.tcx.sess.emit_err(errors::TransparentIncompatible {
hint_spans,
target: target.to_string(),
});
}
if is_explicit_rust && is_c {
let hint_spans: Vec<_> = hint_spans.clone().collect();
self.tcx.sess.emit_err(errors::ReprConflicting { hint_spans });
}
// Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
if (int_reprs > 1)
|| (is_simd && is_c)
Expand All @@ -1910,7 +1917,7 @@ impl CheckAttrVisitor<'_> {
CONFLICTING_REPR_HINTS,
hir_id,
hint_spans.collect::<Vec<Span>>(),
errors::ReprConflicting,
errors::ReprConflictingLint,
);
}
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,16 @@ pub struct ReprIdent {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_repr_conflicting, code = "E0566")]
pub struct ReprConflicting {
#[primary_span]
pub hint_spans: Vec<Span>,
}

#[derive(LintDiagnostic)]
#[diag(passes_repr_conflicting, code = "E0566")]
pub struct ReprConflicting;
pub struct ReprConflictingLint;

#[derive(Diagnostic)]
#[diag(passes_used_static)]
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/repr/repr-c-explicit-rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[repr(C, Rust)] //~ ERROR conflicting representation hints
struct S {
a: i32,
}


#[repr(Rust)] //~ ERROR conflicting representation hints
#[repr(C)]
struct T {
a: i32,
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/repr/repr-c-explicit-rust.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0566]: conflicting representation hints
--> $DIR/repr-c-explicit-rust.rs:1:8
|
LL | #[repr(C, Rust)]
| ^ ^^^^

error[E0566]: conflicting representation hints
--> $DIR/repr-c-explicit-rust.rs:7:8
|
LL | #[repr(Rust)]
| ^^^^
LL | #[repr(C)]
| ^

error: aborting due to 2 previous errors

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

0 comments on commit cf1fba5

Please sign in to comment.