Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions compiler/rustc_builtin_macros/src/eii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use thin_vec::{ThinVec, thin_vec};

use crate::errors::{
EiiExternTargetExpectedList, EiiExternTargetExpectedMacro, EiiExternTargetExpectedUnsafe,
EiiMacroExpectedMaxOneArgument, EiiOnlyOnce, EiiSharedMacroInStatementPosition,
EiiSharedMacroTarget, EiiStaticArgumentRequired, EiiStaticDefault,
EiiStaticMultipleImplementations, EiiStaticMutable,
EiiForbiddenAttr, EiiMacroExpectedMaxOneArgument, EiiOnlyOnce,
EiiSharedMacroInStatementPosition, EiiSharedMacroTarget, EiiStaticArgumentRequired,
EiiStaticDefault, EiiStaticMultipleImplementations, EiiStaticMutable,
};

/// ```rust
Expand Down Expand Up @@ -126,8 +126,7 @@ fn eii_(
let attrs = attrs.clone();
let vis = vis.clone();

let attrs_from_decl =
filter_attrs_for_multiple_eii_attr(ecx, attrs, eii_attr_span, &meta_item.path);
let attrs_from_decl = filter_attrs_for_eii_decl(ecx, attrs, eii_attr_span, &meta_item.path);

let Ok(macro_name) = name_for_impl_macro(ecx, foreign_item_name, &meta_item) else {
// we don't need to wrap in Annotatable::Stmt conditionally since
Expand Down Expand Up @@ -196,8 +195,9 @@ fn name_for_impl_macro(
}
}

/// Ensure that in the list of attrs, there's only a single `eii` attribute.
fn filter_attrs_for_multiple_eii_attr(
/// Reject attributes that cannot be used with externally implementable items,
/// and ensure that in the list of attrs, there's only a single `eii` attribute.
fn filter_attrs_for_eii_decl(
ecx: &mut ExtCtxt<'_>,
attrs: ThinVec<Attribute>,
eii_attr_span: Span,
Expand All @@ -213,6 +213,11 @@ fn filter_attrs_for_multiple_eii_attr(
name: path_to_string(eii_attr_path),
});
false
} else if let Some(name @ (sym::export_name | sym::link_name | sym::no_mangle)) =
i.name()
{
ecx.dcx().emit_err(EiiForbiddenAttr { span: i.span, name });
false
} else {
true
}
Expand Down Expand Up @@ -511,6 +516,15 @@ pub(crate) fn eii_shared_macro(
return vec![item];
};

i.attrs.retain(|attr| {
if let Some(name @ (sym::export_name | sym::link_name | sym::no_mangle)) = attr.name() {
ecx.dcx().emit_err(EiiForbiddenAttr { span: attr.span, name });
false
} else {
true
}
});

let eii_impls = match &mut i.kind {
ItemKind::Fn(func) => &mut func.eii_impls,
ItemKind::Static(stat) => {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,14 @@ pub(crate) struct EiiOnlyOnce {
pub name: String,
}

#[derive(Diagnostic)]
#[diag("`#[{$name}]` cannot be used on externally implementable items")]
pub(crate) struct EiiForbiddenAttr {
#[primary_span]
pub span: Span,
pub name: Symbol,
}

#[derive(Diagnostic)]
#[diag("`#[{$name}]` expected no arguments or a single argument: `#[{$name}(default)]`")]
pub(crate) struct EiiMacroExpectedMaxOneArgument {
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/eii/forbidden_attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Tests attributes that are forbidden on EII.
#![feature(extern_item_impls)]

#[unsafe(no_mangle)]
//~^ ERROR `#[no_mangle]` cannot be used on externally implementable items
#[eii]
fn foo() {}

#[unsafe(export_name = "bar")]
//~^ ERROR `#[export_name]` cannot be used on externally implementable items
#[eii]
fn bar() {}

#[link_name = "baz"]
//~^ ERROR `#[link_name]` cannot be used on externally implementable items
#[eii]
fn baz() {}

#[eii]
fn qux();

#[unsafe(no_mangle)]
//~^ ERROR `#[no_mangle]` cannot be used on externally implementable items
#[qux]
fn qux_impl() {}

#[eii]
fn corge();

#[unsafe(export_name = "corge_impl")]
//~^ ERROR `#[export_name]` cannot be used on externally implementable items
#[corge]
fn corge_impl() {}

#[eii]
fn garply();

#[link_name = "garply_impl"]
//~^ ERROR `#[link_name]` cannot be used on externally implementable items
#[garply]
fn garply_impl() {}

fn main() {}
38 changes: 38 additions & 0 deletions tests/ui/eii/forbidden_attrs.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error: `#[no_mangle]` cannot be used on externally implementable items
--> $DIR/forbidden_attrs.rs:4:1
|
LL | #[unsafe(no_mangle)]
| ^^^^^^^^^^^^^^^^^^^^

error: `#[export_name]` cannot be used on externally implementable items
--> $DIR/forbidden_attrs.rs:9:1
|
LL | #[unsafe(export_name = "bar")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `#[link_name]` cannot be used on externally implementable items
--> $DIR/forbidden_attrs.rs:14:1
|
LL | #[link_name = "baz"]
| ^^^^^^^^^^^^^^^^^^^^

error: `#[no_mangle]` cannot be used on externally implementable items
--> $DIR/forbidden_attrs.rs:22:1
|
LL | #[unsafe(no_mangle)]
| ^^^^^^^^^^^^^^^^^^^^

error: `#[export_name]` cannot be used on externally implementable items
--> $DIR/forbidden_attrs.rs:30:1
|
LL | #[unsafe(export_name = "corge_impl")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `#[link_name]` cannot be used on externally implementable items
--> $DIR/forbidden_attrs.rs:38:1
|
LL | #[link_name = "garply_impl"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors

Loading