Skip to content

Commit

Permalink
fix: #47446
Browse files Browse the repository at this point in the history
- Add test for issue 47446
- Implement the new lint lint_builtin_mixed_export_name_and_no_mangle
- Add suggestion how to fix it
  • Loading branch information
sassman committed Oct 11, 2024
1 parent f496659 commit aa419ee
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ lint_builtin_missing_debug_impl =
lint_builtin_missing_doc = missing documentation for {$article} {$desc}
lint_builtin_mixed_export_name_and_no_mangle = the attribute `export_name` may not be used in combination with `no_mangle`
.label = `export_name` takes precedence
.note = the `no_mangle` attribute is ignored
.suggestion = remove the `no_mangle` attribute
lint_builtin_mutable_transmutes =
transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
Expand Down
64 changes: 64 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3035,3 +3035,67 @@ impl EarlyLintPass for SpecialModuleName {
}
}
}

declare_lint! {
/// The `mixed_export_name_and_no_mangle` lint detects mixed usage of `export_name` and `no_mangle`
/// where `no_mangle` is not used by the compiler.
///
/// ### Example
///
/// ```rust,compile_fail
/// #[no_mangle]
/// #[export_name = "foo"]
/// pub fn bar() {}
///
/// fn main() {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// The compiler will not use the `no_mangle` attribute when generating the symbol name for the function,
/// as the `export_name` attribute is used instead. This can lead to confusion and is unnecessary.
///
MIXED_EXPORT_NAME_AND_NO_MANGLE,
Warn,
"mixed usage of export_name and no_mangle, where no_mangle is not used by the compiler"
}

declare_lint_pass!(MixedExportNameAndNoMangle => [MIXED_EXPORT_NAME_AND_NO_MANGLE]);

impl MixedExportNameAndNoMangle {
fn report_mixed_export_name_and_no_mangle(
&self,
cx: &EarlyContext<'_>,
span_export_name: Span,
span_no_mangle: Span,
) {
let decorate = crate::lints::BuiltinMixedExportNameAndNoMangle {
export_name: span_export_name,
no_mangle: span_no_mangle.clone(),
removal_span: span_no_mangle,
};
cx.emit_span_lint(MIXED_EXPORT_NAME_AND_NO_MANGLE, span_export_name, decorate);
}
}

impl EarlyLintPass for MixedExportNameAndNoMangle {
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
match it.kind {
ast::ItemKind::Fn(..) | ast::ItemKind::Static(..) => {
let no_mangle = attr::find_by_name(&it.attrs, sym::no_mangle);
let export_name = attr::find_by_name(&it.attrs, sym::export_name);
if let (Some(no_mangle_attr), Some(export_name_attr)) = (no_mangle, export_name) {
self.report_mixed_export_name_and_no_mangle(
cx,
export_name_attr.span,
no_mangle_attr.span,
);
}
}

_ => {}
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ early_lint_methods!(
UnusedDocComment: UnusedDocComment,
Expr2024: Expr2024,
Precedence: Precedence,
MixedExportNameAndNoMangle: MixedExportNameAndNoMangle,
]
]
);
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3060,3 +3060,15 @@ pub(crate) struct ReservedString {
#[suggestion(code = " ", applicability = "machine-applicable")]
pub suggestion: Span,
}

#[diag(lint_builtin_mixed_export_name_and_no_mangle)]
pub(crate) struct BuiltinMixedExportNameAndNoMangle {
#[label]
pub export_name: Span,

#[note]
pub no_mangle: Span,

#[suggestion(style = "verbose", code = "", applicability = "machine-applicable")]
pub removal_span: Span,
}
7 changes: 7 additions & 0 deletions tests/ui/attributes/mixed_export_name_and_no_mangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// issue: rust-lang/rust#47446

#[no_mangle]
#[export_name = "foo"]
pub fn bar() {}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/attributes/mixed_export_name_and_no_mangle.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
warning: the attribute `export_name` may not be used in combination with `no_mangle`
--> $DIR/E47446.rs:2:1
|
LL | #[export_name = "foo"]
| ^^^^^^^^^^^^^^^^^^^^^^ `export_name` takes precedence
|
= note: when `export_name` is used `no_mangle` is ignored
note: the `no_mangle` attribute is ignored
--> $DIR/E47446.rs:1:1
|
LL | #[no_mangle]
| ^^^^^^^^^^^^
= note: `#[warn(mixed_export_name_and_no_mangle)]` on by default
help: remove the `no_mangle` attribute
|
LL - #[no_mangle]
|

0 comments on commit aa419ee

Please sign in to comment.