Setup unstable feature bound attribute

This commit is contained in:
tiif
2025-07-14 13:30:36 +00:00
parent a9fb6103b0
commit fecd99881d
7 changed files with 123 additions and 0 deletions

View File

@@ -667,6 +667,10 @@ passes_rustc_std_internal_symbol =
attribute should be applied to functions or statics
.label = not a function or static
passes_rustc_unstable_feature_bound =
attribute should be applied to `impl` or free function outside of any `impl` or trait
.label = not an `impl` or free function
passes_should_be_applied_to_fn =
attribute should be applied to a function definition
.label = {$on_crate ->

View File

@@ -247,6 +247,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
&Attribute::Parsed(AttributeKind::FfiPure(attr_span)) => {
self.check_ffi_pure(attr_span, attrs, target)
}
Attribute::Parsed(AttributeKind::UnstableFeatureBound(syms)) => {
self.check_unstable_feature_bound(syms.first().unwrap().1, span, target)
}
Attribute::Parsed(
AttributeKind::BodyStability { .. }
| AttributeKind::ConstStabilityIndirect
@@ -2267,6 +2270,47 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
fn check_unstable_feature_bound(&self, attr_span: Span, span: Span, target: Target) {
match target {
// FIXME(staged_api): There's no reason we can't support more targets here. We're just
// being conservative to begin with.
Target::Fn | Target::Impl => {}
Target::ExternCrate
| Target::Use
| Target::Static
| Target::Const
| Target::Closure
| Target::Mod
| Target::ForeignMod
| Target::GlobalAsm
| Target::TyAlias
| Target::Enum
| Target::Variant
| Target::Struct
| Target::Field
| Target::Union
| Target::Trait
| Target::TraitAlias
| Target::Expression
| Target::Statement
| Target::Arm
| Target::AssocConst
| Target::Method(_)
| Target::AssocTy
| Target::ForeignFn
| Target::ForeignStatic
| Target::ForeignTy
| Target::GenericParam(_)
| Target::MacroDef
| Target::Param
| Target::PatField
| Target::ExprField
| Target::WherePredicate => {
self.tcx.dcx().emit_err(errors::RustcUnstableFeatureBound { attr_span, span });
}
}
}
fn check_rustc_std_internal_symbol(&self, attr_span: Span, span: Span, target: Target) {
match target {
Target::Fn | Target::Static | Target::ForeignFn | Target::ForeignStatic => {}

View File

@@ -686,6 +686,15 @@ pub(crate) struct RustcAllowConstFnUnstable {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(passes_rustc_unstable_feature_bound)]
pub(crate) struct RustcUnstableFeatureBound {
#[primary_span]
pub attr_span: Span,
#[label]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(passes_rustc_std_internal_symbol)]
pub(crate) struct RustcStdInternalSymbol {