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
19 changes: 10 additions & 9 deletions compiler/rustc_attr_ir/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,16 @@ pub enum AttributeKind {
/// Represents `#[linkage]`.
Linkage(Linkage, Span),

/// Represents `#[lint::as_ptr]` (used by the `dangling_pointers_from_temporaries` lint).
LintAsPtr,

/// Represents `#[lint::never_returns_null_ptr]` (used by the `useless_ptr_null_checks` lint).
LintNeverReturnsNullPtr,

/// Represents `#[lint::should_not_be_called_on_const_items]` (used by the
/// `const_item_interior_mutations` lint).
LintShouldNotBeCalledOnConstItems,

/// Represents `#[loop_match]`.
LoopMatch(Span),

Expand Down Expand Up @@ -1142,9 +1152,6 @@ pub enum AttributeKind {
/// Represents `#[rustc_allow_lifetime_dependent_specialization]`.
RustcAllowLifetimeDependentSpecialization,

/// Represents `#[rustc_as_ptr]` (used by the `dangling_pointers_from_temporaries` lint).
RustcAsPtr,

/// Represents `#[rustc_autodiff]`.
RustcAutodiff(Option<Box<RustcAutodiff>>),

Expand Down Expand Up @@ -1333,9 +1340,6 @@ pub enum AttributeKind {
/// Represents `#[rustc_must_match_exhaustively]`
RustcMustMatchExhaustively(Span),

/// Represents `#[rustc_never_returns_null_ptr]`
RustcNeverReturnsNullPtr,

/// Represents `#[rustc_no_implicit_autorefs]`
RustcNoImplicitAutorefs,

Expand Down Expand Up @@ -1404,9 +1408,6 @@ pub enum AttributeKind {
element_count: Option<u16>,
},

/// Represents `#[rustc_should_not_be_called_on_const_items]`
RustcShouldNotBeCalledOnConstItems,

/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
RustcSimdMonomorphizeLaneLimit(Limit),

Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_attr_ir/src/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl AttributeKind {
LinkOrdinal { .. } => No,
LinkSection { .. } => Yes, // Needed for rustdoc
Linkage(..) => No,
LintAsPtr => Yes,
LintNeverReturnsNullPtr => Yes,
LintShouldNotBeCalledOnConstItems => Yes,
LoopMatch(..) => No,
MacroEscape => No,
MacroExport { .. } => Yes,
Expand Down Expand Up @@ -108,7 +111,6 @@ impl AttributeKind {
RustcAllowConstFnUnstable(..) => No,
RustcAllowIncoherentImpl(..) => No,
RustcAllowLifetimeDependentSpecialization => No,
RustcAsPtr => Yes,
RustcAutodiff(..) => Yes,
RustcBodyStability { .. } => No,
RustcBuiltinMacro { .. } => Yes,
Expand Down Expand Up @@ -165,7 +167,6 @@ impl AttributeKind {
RustcMir(..) => Yes,
RustcMustImplementOneOf { .. } => No,
RustcMustMatchExhaustively(..) => Yes,
RustcNeverReturnsNullPtr => Yes,
RustcNoImplicitAutorefs => Yes,
RustcNoImplicitBounds => No,
RustcNoMirInline => Yes,
Expand All @@ -186,7 +187,6 @@ impl AttributeKind {
RustcReallocator => No,
RustcRegions => No,
RustcScalableVector { .. } => Yes,
RustcShouldNotBeCalledOnConstItems => Yes,
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
RustcSkipDuringMethodDispatch { .. } => No,
RustcSpecializationTrait => No,
Expand Down Expand Up @@ -272,6 +272,9 @@ impl AttributeKind {
| crate::Linkage::WeakAny
| crate::Linkage::WeakODR => true,
},
LintAsPtr => false,
LintNeverReturnsNullPtr => false,
LintShouldNotBeCalledOnConstItems => false,
LoopMatch(..) => false,
MacroEscape => false,
MacroExport { .. } => false,
Expand Down Expand Up @@ -322,7 +325,6 @@ impl AttributeKind {
RustcAllowConstFnUnstable(..) => false,
RustcAllowIncoherentImpl(..) => false,
RustcAllowLifetimeDependentSpecialization => false,
RustcAsPtr => false,
RustcAutodiff(..) => false,
RustcBodyStability { .. } => false,
RustcBuiltinMacro { .. } => false,
Expand Down Expand Up @@ -379,7 +381,6 @@ impl AttributeKind {
RustcMir(..) => false,
RustcMustImplementOneOf { .. } => false,
RustcMustMatchExhaustively(..) => false,
RustcNeverReturnsNullPtr => false,
RustcNoImplicitAutorefs => false,
RustcNoImplicitBounds => false,
RustcNoMirInline => false,
Expand All @@ -400,7 +401,7 @@ impl AttributeKind {
RustcReallocator => false,
RustcRegions => false,
RustcScalableVector { .. } => false,
RustcShouldNotBeCalledOnConstItems => false,

RustcSimdMonomorphizeLaneLimit(..) => false,
RustcSkipDuringMethodDispatch { .. } => false,
RustcSpecializationTrait => false,
Expand Down
32 changes: 24 additions & 8 deletions compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use rustc_feature::AttributeStability;
use super::prelude::*;
use crate::diagnostics::RustcPubTransparent;

pub(crate) struct RustcAsPtrParser;
impl NoArgsAttributeParser for RustcAsPtrParser {
const PATH: &[Symbol] = &[sym::rustc_as_ptr];
pub(crate) struct LintAsPtrParser;
impl NoArgsAttributeParser for LintAsPtrParser {
const PATH: &[Symbol] = &[sym::lint, sym::as_ptr];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Fn),
Allow(Target::Method(MethodKind::Inherent)),
Expand All @@ -15,7 +15,23 @@ impl NoArgsAttributeParser for RustcAsPtrParser {
Allow(Target::Method(MethodKind::TraitImpl)),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcAsPtr;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintAsPtr;
}

pub(crate) struct LintNeverReturnsNullPtrParser;

impl NoArgsAttributeParser for LintNeverReturnsNullPtrParser {
const PATH: &[Symbol] = &[sym::lint, sym::never_returns_null_ptr];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Fn),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);

const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintNeverReturnsNullPtr;
}

pub(crate) struct RustcPubTransparentParser;
Expand Down Expand Up @@ -53,15 +69,15 @@ impl NoArgsAttributeParser for RustcPassByValueParser {
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcPassByValue;
}

pub(crate) struct RustcShouldNotBeCalledOnConstItemsParser;
impl NoArgsAttributeParser for RustcShouldNotBeCalledOnConstItemsParser {
const PATH: &[Symbol] = &[sym::rustc_should_not_be_called_on_const_items];
pub(crate) struct LintShouldNotBeCalledOnConstItemsParser;
impl NoArgsAttributeParser for LintShouldNotBeCalledOnConstItemsParser {
const PATH: &[Symbol] = &[sym::lint, sym::should_not_be_called_on_const_items];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::TraitImpl)),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcShouldNotBeCalledOnConstItems;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintShouldNotBeCalledOnConstItems;
}

pub(crate) struct AutomaticallyDerivedParser;
Expand Down
16 changes: 0 additions & 16 deletions compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ impl SingleAttributeParser for RustcMustImplementOneOfParser {
}
}

pub(crate) struct RustcNeverReturnsNullPtrParser;

impl NoArgsAttributeParser for RustcNeverReturnsNullPtrParser {
const PATH: &[Symbol] = &[sym::rustc_never_returns_null_ptr];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Fn),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);

const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNeverReturnsNullPtr;
}

pub(crate) struct RustcPanicsWhenZeroParser;

impl NoArgsAttributeParser for RustcPanicsWhenZeroParser {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ attribute_parsers!(
Single<WithoutArgs<FfiConstParser>>,
Single<WithoutArgs<FfiPureParser>>,
Single<WithoutArgs<FundamentalParser>>,
Single<WithoutArgs<LintAsPtrParser>>,
Single<WithoutArgs<LintNeverReturnsNullPtrParser>>,
Single<WithoutArgs<LintShouldNotBeCalledOnConstItemsParser>>,
Single<WithoutArgs<LoopMatchParser>>,
Single<WithoutArgs<MacroEscapeParser>>,
Single<WithoutArgs<MarkerParser>>,
Expand All @@ -296,7 +299,6 @@ attribute_parsers!(
Single<WithoutArgs<RustcAllocatorZeroedParser>>,
Single<WithoutArgs<RustcAllowIncoherentImplParser>>,
Single<WithoutArgs<RustcAllowLifetimeDependentSpecializationParser>>,
Single<WithoutArgs<RustcAsPtrParser>>,
Single<WithoutArgs<RustcCanonicalSymbolParser>>,
Single<WithoutArgs<RustcCaptureAnalysisParser>>,
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
Expand Down Expand Up @@ -332,7 +334,6 @@ attribute_parsers!(
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,
Single<WithoutArgs<RustcMainParser>>,
Single<WithoutArgs<RustcNeverReturnsNullPtrParser>>,
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
Single<WithoutArgs<RustcNoMirInlineParser>>,
Expand All @@ -350,7 +351,6 @@ attribute_parsers!(
Single<WithoutArgs<RustcPubTransparentParser>>,
Single<WithoutArgs<RustcReallocatorParser>>,
Single<WithoutArgs<RustcRegionsParser>>,
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItemsParser>>,
Single<WithoutArgs<RustcSpecializationTraitParser>>,
Single<WithoutArgs<RustcStdInternalSymbolParser>>,
Single<WithoutArgs<RustcStrictCoherenceParser>>,
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_attr_parsing/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2077,3 +2077,11 @@ pub(crate) enum MacroExport {
#[note("declarative macros follow the same exporting rules as regular items")]
OnDeclMacro,
}

#[derive(Diagnostic)]
#[diag("the `lint::{$unknown}` attribute is not recognized")]
pub(crate) struct UnknownLintHelper {
#[primary_span]
pub span: Span,
pub unknown: Symbol,
}
19 changes: 18 additions & 1 deletion compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::context::{
use crate::diagnostics::ParsedDescription;
use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
use crate::synthetic::SyntheticAttrState;
use crate::{AttributeTemplate, ShouldEmit};
use crate::{AttributeTemplate, ShouldEmit, diagnostics};

pub struct EmitAttribute(
pub Box<
Expand Down Expand Up @@ -452,6 +452,23 @@ impl<'sess> AttributeParser<'sess> {
}
} else if let [sym::diagnostic, _unknown, ..] = &*parts {
self.unknown_diagnostic_attr(&n.item.path.segments[1], &mut emit_lint);
} else if let [sym::lint, unknown, ..] = &*parts {
// Avoid cascading errors. If the `lint` tool has not been registered,
// we could be here in the `#[lint::attribute_macro] not found` case,
// which recovers with a `NonMacroAttr` res.
if let Some(tools) = self.attr_tools
&& tools.iter().any(|i| i.name == sym::lint)
{
self.emit_err(diagnostics::UnknownLintHelper {
span: n.item.span,
unknown: *unknown,
});
} else {
self.dcx().span_delayed_bug(
n.item.span,
"encountered `lint` tool attribute but error not reported",
);
}
} else {
let attr = AttrItem {
path: attr_path.clone(),
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,7 @@ pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[
// Internal attributes, Misc:
// ==========================================================================
sym::lang,
sym::rustc_as_ptr,
sym::rustc_should_not_be_called_on_const_items,
sym::rustc_pass_by_value,
sym::rustc_never_returns_null_ptr,
sym::rustc_no_implicit_autorefs,
sym::rustc_coherence_is_core,
sym::rustc_coinductive,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/dangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
&& let ty = cx.typeck_results().expr_ty(receiver)
&& owns_allocation(cx.tcx, ty)
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& find_attr!(cx.tcx, fn_id, RustcAsPtr)
&& find_attr!(cx.tcx, fn_id, LintAsPtr)
{
cx.tcx.emit_node_span_lint(
DANGLING_POINTERS_FROM_TEMPORARIES,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/interior_mutable_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for InteriorMutableConsts {
// Let's do the attribute check after the other checks for perf reasons
&& find_attr!(
cx.tcx, method_did,
RustcShouldNotBeCalledOnConstItems
LintShouldNotBeCalledOnConstItems
)
&& let Some(method_name) = cx.tcx.opt_item_ident(method_did)
&& let Some(const_name) = cx.tcx.opt_item_ident(const_did)
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/ptr_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS, INVALID_NULL_ARGUM
/// This function checks if the expression is from a series of consecutive casts,
/// i.e. `(my_fn as *const _ as *mut _).cast_mut()` and whether the original expression is either
/// a fn ptr, a reference, or a function call whose definition is
/// annotated with `#![rustc_never_returns_null_ptr]`.
/// annotated with `#[lint::never_returns_null_ptr]`.
/// If this situation is present, the function returns the appropriate diagnostic.
fn useless_check<'a, 'tcx: 'a>(
cx: &'a LateContext<'tcx>,
Expand All @@ -73,14 +73,14 @@ fn useless_check<'a, 'tcx: 'a>(
e = e.peel_blocks();
if let ExprKind::MethodCall(_, _expr, [], _) = e.kind
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
&& find_attr!(cx.tcx, def_id, RustcNeverReturnsNullPtr)
&& find_attr!(cx.tcx, def_id, LintNeverReturnsNullPtr)
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
{
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
} else if let ExprKind::Call(path, _args) = e.kind
&& let ExprKind::Path(ref qpath) = path.kind
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
&& find_attr!(cx.tcx, def_id, RustcNeverReturnsNullPtr)
&& find_attr!(cx.tcx, def_id, LintNeverReturnsNullPtr)
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
{
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::LinkName { .. } => (),
AttributeKind::LinkOrdinal { .. } => (),
AttributeKind::LinkSection { .. } => (),
AttributeKind::LintAsPtr => (),
AttributeKind::LintNeverReturnsNullPtr => (),
AttributeKind::LintShouldNotBeCalledOnConstItems => (),
AttributeKind::LoopMatch(..) => (),
AttributeKind::MacroEscape => (),
AttributeKind::MacroExport { .. } => (),
Expand Down Expand Up @@ -308,7 +311,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::RustcAllocatorZeroedVariant { .. } => (),
AttributeKind::RustcAllowIncoherentImpl(..) => (),
AttributeKind::RustcAllowLifetimeDependentSpecialization => (),
AttributeKind::RustcAsPtr => (),
AttributeKind::RustcAutodiff(..) => (),
AttributeKind::RustcBodyStability { .. } => (),
AttributeKind::RustcBuiltinMacro { .. } => (),
Expand Down Expand Up @@ -363,7 +365,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::RustcMain => (),
AttributeKind::RustcMir(_) => (),
AttributeKind::RustcMustMatchExhaustively(..) => (),
AttributeKind::RustcNeverReturnsNullPtr => (),
AttributeKind::RustcNoImplicitAutorefs => (),
AttributeKind::RustcNoImplicitBounds => (),
AttributeKind::RustcNoMirInline => (),
Expand All @@ -384,10 +385,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::RustcReallocator => (),
AttributeKind::RustcRegions => (),
AttributeKind::RustcScalableVector { .. } => (),
AttributeKind::RustcShouldNotBeCalledOnConstItems => (),
AttributeKind::RustcSimdMonomorphizeLaneLimit(..) => (),
AttributeKind::RustcSkipDuringMethodDispatch { .. } => (),

AttributeKind::RustcSpecializationTrait => (),
AttributeKind::RustcStdInternalSymbol => (),
AttributeKind::RustcStrictCoherence(..) => (),
Expand Down
Loading
Loading