mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Rollup merge of #163787 - ssenthilnathan3:move-legacy-const-generics-check, r=JonathanBrouwer
moves rustc_legacy_const_generics checks into attribute parsing Part of rust-lang/rust#153101. Moves the remaining `#[rustc_legacy_const_generics]` validation from `rustc_passes::check_attr` into `RustcLegacyConstGenericsParser::finalize_check`. The three associated diagnostics are moved into `rustc_attr_parsing`, and the old check is removed from `check_attr.rs`. Diagnostic messages and spans are preserved.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
|
||||
use rustc_ast::{GenericParamKind, ItemKind, LitIntType, LitKind, MetaItemLit};
|
||||
use rustc_attr_ir::lang_items::LangItem;
|
||||
use rustc_attr_ir::{
|
||||
BorrowckGraphvizFormatKind, CguFields, CguKind, RustcCleanAttribute, RustcCleanQueries,
|
||||
@@ -169,6 +169,48 @@ impl SingleAttributeParser for RustcLegacyConstGenericsParser {
|
||||
attr_span: cx.attr_span,
|
||||
})
|
||||
}
|
||||
|
||||
fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
|
||||
if cx.target != Target::Fn {
|
||||
// Invalid targets are already diagnosed by target checking.
|
||||
return;
|
||||
}
|
||||
let item = cx.target_item.expect("missing AST target item for Target::Fn");
|
||||
let ItemKind::Fn(function) = &item.kind else {
|
||||
panic!("expected fn AST target item for Target::Fn");
|
||||
};
|
||||
let index_list = rustc_attr_ir::find_attr!(cx.parsed_attrs, RustcLegacyConstGenerics { fn_indexes, .. } => fn_indexes)
|
||||
.expect("missing parsed RustcLegacyConstGenerics attribute in finalize_check");
|
||||
let generics = &function.generics;
|
||||
|
||||
for param in &generics.params {
|
||||
if !matches!(param.kind, GenericParamKind::Const { .. }) {
|
||||
cx.emit_err(diagnostics::RustcLegacyConstGenericsOnly {
|
||||
attr_span,
|
||||
param_span: param.span(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if index_list.len() != generics.params.len() {
|
||||
cx.emit_err(diagnostics::RustcLegacyConstGenericsIndex {
|
||||
attr_span,
|
||||
generics_span: generics.span,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let arg_count = function.sig.decl.inputs.len() + generics.params.len();
|
||||
for (index, span) in index_list {
|
||||
if *index >= arg_count {
|
||||
cx.emit_err(diagnostics::RustcLegacyConstGenericsIndexExceed {
|
||||
span: *span,
|
||||
arg_count,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RustcInheritOverflowChecksParser;
|
||||
|
||||
@@ -12,6 +12,41 @@ use rustc_target::spec::TargetTuple;
|
||||
use crate::AttributeTemplate;
|
||||
use crate::context::Suggestion;
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("#[rustc_legacy_const_generics] functions must only have const generics")]
|
||||
pub(crate) struct RustcLegacyConstGenericsOnly {
|
||||
#[primary_span]
|
||||
pub attr_span: Span,
|
||||
#[label("non-const generic parameter")]
|
||||
pub param_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("#[rustc_legacy_const_generics] must have one index for each generic parameter")]
|
||||
pub(crate) struct RustcLegacyConstGenericsIndex {
|
||||
#[primary_span]
|
||||
pub attr_span: Span,
|
||||
#[label("generic parameters")]
|
||||
pub generics_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("index exceeds number of arguments")]
|
||||
pub(crate) struct RustcLegacyConstGenericsIndexExceed {
|
||||
#[primary_span]
|
||||
#[label(
|
||||
"there {$arg_count ->
|
||||
[one] is
|
||||
*[other] are
|
||||
} only {$arg_count} {$arg_count ->
|
||||
[one] argument
|
||||
*[other] arguments
|
||||
}"
|
||||
)]
|
||||
pub span: Span,
|
||||
pub arg_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("`{$name}` attribute cannot be used at crate level")]
|
||||
pub(crate) struct InvalidAttrAtCrateLevel {
|
||||
|
||||
@@ -24,8 +24,8 @@ use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::LocalModId;
|
||||
use rustc_hir::intravisit::{self, Visitor};
|
||||
use rustc_hir::{
|
||||
self as hir, CRATE_HIR_ID, Constness, FnSig, ForeignItem, GenericParam, GenericParamKind,
|
||||
HirId, Item, ItemKind, Mod, Node, ParamName, TraitItem,
|
||||
self as hir, CRATE_HIR_ID, Constness, ForeignItem, GenericParam, GenericParamKind, HirId, Item,
|
||||
ItemKind, Mod, Node, ParamName, TraitItem,
|
||||
};
|
||||
use rustc_lint_defs::builtin::{
|
||||
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_ATTRIBUTES,
|
||||
@@ -203,9 +203,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
||||
}
|
||||
AttributeKind::Naked(..) => self.check_naked(hir_id, target),
|
||||
AttributeKind::MayDangle(attr_span) => self.check_may_dangle(hir_id, *attr_span),
|
||||
AttributeKind::RustcLegacyConstGenerics { attr_span, fn_indexes } => {
|
||||
self.check_rustc_legacy_const_generics(item, *attr_span, fn_indexes)
|
||||
}
|
||||
AttributeKind::Doc(attr) => self.check_doc_attrs(attr, hir_id, target),
|
||||
AttributeKind::EiiImpl(eii_impl) => self.check_eii_impl(eii_impl),
|
||||
AttributeKind::RustcMustImplementOneOf { attr_span, fn_names } => {
|
||||
@@ -355,6 +352,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
||||
AttributeKind::RustcInsignificantDtor => (),
|
||||
AttributeKind::RustcIntrinsic => (),
|
||||
AttributeKind::RustcIntrinsicConstStableIndirect => (),
|
||||
AttributeKind::RustcLegacyConstGenerics { .. } => (),
|
||||
AttributeKind::RustcLintOptDenyFieldAccess { .. } => (),
|
||||
AttributeKind::RustcLintOptTy => (),
|
||||
AttributeKind::RustcLintQueryInstability => (),
|
||||
@@ -1065,51 +1063,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
||||
self.dcx().emit_err(diagnostics::InvalidMayDangle { attr_span });
|
||||
}
|
||||
|
||||
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
|
||||
fn check_rustc_legacy_const_generics(
|
||||
&self,
|
||||
item: Option<&'tcx Item<'tcx>>,
|
||||
attr_span: Span,
|
||||
index_list: &ThinVec<(usize, Span)>,
|
||||
) {
|
||||
let Some(Item { kind: ItemKind::Fn { sig: FnSig { decl, .. }, generics, .. }, .. }) = item
|
||||
else {
|
||||
// No error here, since it's already given by the parser
|
||||
return;
|
||||
};
|
||||
|
||||
for param in generics.params {
|
||||
match param.kind {
|
||||
hir::GenericParamKind::Const { .. } => {}
|
||||
_ => {
|
||||
self.dcx().emit_err(diagnostics::RustcLegacyConstGenericsOnly {
|
||||
attr_span,
|
||||
param_span: param.span,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if index_list.len() != generics.params.len() {
|
||||
self.dcx().emit_err(diagnostics::RustcLegacyConstGenericsIndex {
|
||||
attr_span,
|
||||
generics_span: generics.span,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let arg_count = decl.inputs.len() + generics.params.len();
|
||||
for (index, span) in index_list {
|
||||
if *index >= arg_count {
|
||||
self.dcx().emit_err(diagnostics::RustcLegacyConstGenericsIndexExceed {
|
||||
span: *span,
|
||||
arg_count,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the `#[repr]` attributes on `item` are valid.
|
||||
fn check_repr(
|
||||
&self,
|
||||
|
||||
@@ -126,41 +126,6 @@ pub(crate) struct BothOptimizeNoneAndInline {
|
||||
pub inline_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("#[rustc_legacy_const_generics] functions must only have const generics")]
|
||||
pub(crate) struct RustcLegacyConstGenericsOnly {
|
||||
#[primary_span]
|
||||
pub attr_span: Span,
|
||||
#[label("non-const generic parameter")]
|
||||
pub param_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("#[rustc_legacy_const_generics] must have one index for each generic parameter")]
|
||||
pub(crate) struct RustcLegacyConstGenericsIndex {
|
||||
#[primary_span]
|
||||
pub attr_span: Span,
|
||||
#[label("generic parameters")]
|
||||
pub generics_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("index exceeds number of arguments")]
|
||||
pub(crate) struct RustcLegacyConstGenericsIndexExceed {
|
||||
#[primary_span]
|
||||
#[label(
|
||||
"there {$arg_count ->
|
||||
[one] is
|
||||
*[other] are
|
||||
} only {$arg_count} {$arg_count ->
|
||||
[one] argument
|
||||
*[other] arguments
|
||||
}"
|
||||
)]
|
||||
pub span: Span,
|
||||
pub arg_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("conflicting representation hints", code = E0566)]
|
||||
pub(crate) struct ReprConflicting {
|
||||
|
||||
@@ -1,3 +1,23 @@
|
||||
error: #[rustc_legacy_const_generics] must have one index for each generic parameter
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:3:1
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(0)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | fn foo1() {}
|
||||
| - generic parameters
|
||||
|
||||
error: index exceeds number of arguments
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:6:31
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(1)]
|
||||
| ^ there is only 1 argument
|
||||
|
||||
error: index exceeds number of arguments
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:9:31
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(2)]
|
||||
| ^ there are only 2 arguments
|
||||
|
||||
error[E0539]: malformed `rustc_legacy_const_generics` attribute input
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:12:3
|
||||
|
|
||||
@@ -78,6 +98,14 @@ LL | #[rustc_legacy_const_generics(1)]
|
||||
|
|
||||
= help: the `rustc_legacy_const_generics` attribute can only be applied to functions
|
||||
|
||||
error: #[rustc_legacy_const_generics] functions must only have const generics
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:33:1
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(0)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | fn foo8<X>() {}
|
||||
| - non-const generic parameter
|
||||
|
||||
error: the `rustc_legacy_const_generics` attribute cannot be used on inherent methods
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:37:7
|
||||
|
|
||||
@@ -111,34 +139,6 @@ LL - #[rustc_legacy_const_generics = 1]
|
||||
LL + #[rustc_legacy_const_generics(N)]
|
||||
|
|
||||
|
||||
error: #[rustc_legacy_const_generics] must have one index for each generic parameter
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:3:1
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(0)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | fn foo1() {}
|
||||
| - generic parameters
|
||||
|
||||
error: index exceeds number of arguments
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:6:31
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(1)]
|
||||
| ^ there is only 1 argument
|
||||
|
||||
error: index exceeds number of arguments
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:9:31
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(2)]
|
||||
| ^ there are only 2 arguments
|
||||
|
||||
error: #[rustc_legacy_const_generics] functions must only have const generics
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:33:1
|
||||
|
|
||||
LL | #[rustc_legacy_const_generics(0)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | fn foo8<X>() {}
|
||||
| - non-const generic parameter
|
||||
|
||||
error[E0044]: foreign items may not have const parameters
|
||||
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:30:5
|
||||
|
|
||||
|
||||
Reference in New Issue
Block a user