Skip to content

Commit

Permalink
Gate and validate #[rustc_safe_intrinsic]
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Sep 25, 2023
1 parent aadb571 commit 56c9ff5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
"allow_internal_unsafe side-steps the unsafe_code lint",
),
ungated!(rustc_safe_intrinsic, Normal, template!(Word), DuplicatesOk),
rustc_attr!(rustc_allowed_through_unstable_modules, Normal, template!(Word), WarnFollowing,
"rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
through unstable paths"),
Expand Down Expand Up @@ -806,6 +805,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_doc_primitive, Normal, template!(NameValueStr: "primitive name"), ErrorFollowing,
r#"`rustc_doc_primitive` is a rustc internal attribute"#,
),
rustc_attr!(
rustc_safe_intrinsic, Normal, template!(Word), WarnFollowing,
"the `#[rustc_safe_intrinsic]` attribute is used internally to mark intrinsics as safe"
),

// ==========================================================================
// Internal attributes, Testing:
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,10 @@ passes_rustc_lint_opt_ty =
`#[rustc_lint_opt_ty]` should be applied to a struct
.label = not a struct
passes_rustc_safe_intrinsic =
attribute should be applied to intrinsic functions
.label = not an intrinsic function
passes_rustc_std_internal_symbol =
attribute should be applied to functions or statics
.label = not a function or static
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ impl CheckAttrVisitor<'_> {
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
sym::link_ordinal => self.check_link_ordinal(&attr, span, target),
sym::rustc_confusables => self.check_confusables(&attr, target),
sym::rustc_safe_intrinsic => {
self.check_rustc_safe_intrinsic(hir_id, attr, span, target)
}
_ => true,
};

Expand Down Expand Up @@ -2042,6 +2045,29 @@ impl CheckAttrVisitor<'_> {
}
}

fn check_rustc_safe_intrinsic(
&self,
hir_id: HirId,
attr: &Attribute,
span: Span,
target: Target,
) -> bool {
let hir = self.tcx.hir();

if let Target::ForeignFn = target
&& let Some(parent) = hir.opt_parent_id(hir_id)
&& let hir::Node::Item(Item {
kind: ItemKind::ForeignMod { abi: Abi::RustIntrinsic | Abi::PlatformIntrinsic, .. },
..
}) = hir.get(parent)
{
return true;
}

self.tcx.sess.emit_err(errors::RustcSafeIntrinsic { attr_span: attr.span, span });
false
}

fn check_rustc_std_internal_symbol(
&self,
attr: &Attribute,
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ pub struct RustcAllowConstFnUnstable {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_rustc_safe_intrinsic)]
pub struct RustcSafeIntrinsic {
#[primary_span]
pub attr_span: Span,
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_rustc_std_internal_symbol)]
pub struct RustcStdInternalSymbol {
Expand Down

0 comments on commit 56c9ff5

Please sign in to comment.