Port #[rustc_dump_vtable]

This commit is contained in:
Jonathan Brouwer 2026-01-12 19:51:50 +01:00
parent 2a455409e3
commit a4c34b421c
No known key found for this signature in database
GPG key ID: F13E55D38C971DEF
6 changed files with 31 additions and 11 deletions

View file

@ -33,3 +33,15 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpItemBounds {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::AssocTy)]);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpItemBounds;
}
pub(crate) struct RustcDumpVtable;
impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpVtable {
const PATH: &[Symbol] = &[sym::rustc_dump_vtable];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
Allow(Target::Impl { of_trait: true }),
Allow(Target::TyAlias),
]);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcDumpVtable;
}

View file

@ -63,7 +63,9 @@ use crate::attributes::proc_macro_attrs::{
};
use crate::attributes::prototype::CustomMirParser;
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
use crate::attributes::rustc_dump::{RustcDumpDefParents, RustcDumpItemBounds, RustcDumpUserArgs};
use crate::attributes::rustc_dump::{
RustcDumpDefParents, RustcDumpItemBounds, RustcDumpUserArgs, RustcDumpVtable,
};
use crate::attributes::rustc_internal::{
RustcHasIncoherentInherentImplsParser, RustcLayoutScalarValidRangeEndParser,
RustcLayoutScalarValidRangeStartParser, RustcLegacyConstGenericsParser,
@ -271,6 +273,7 @@ attribute_parsers!(
Single<WithoutArgs<RustcDumpDefParents>>,
Single<WithoutArgs<RustcDumpItemBounds>>,
Single<WithoutArgs<RustcDumpUserArgs>>,
Single<WithoutArgs<RustcDumpVtable>>,
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
Single<WithoutArgs<RustcLintDiagnosticsParser>>,
Single<WithoutArgs<RustcLintOptTyParser>>,

View file

@ -915,6 +915,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_dump_user_args]`
RustcDumpUserArgs,
/// Represents `#[rustc_dump_vtable]`
RustcDumpVtable(Span),
/// Represents `#[rustc_has_incoherent_inherent_impls]`
RustcHasIncoherentInherentImpls,

View file

@ -100,6 +100,7 @@ impl AttributeKind {
RustcDumpDefParents => No,
RustcDumpItemBounds => No,
RustcDumpUserArgs => No,
RustcDumpVtable(..) => No,
RustcHasIncoherentInherentImpls => Yes,
RustcLayoutScalarValidRangeEnd(..) => Yes,
RustcLayoutScalarValidRangeStart(..) => Yes,

View file

@ -103,7 +103,9 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
for id in tcx.hir_free_items() {
let def_id = id.owner_id.def_id;
let Some(attr) = tcx.get_attr(def_id, sym::rustc_dump_vtable) else {
let Some(&attr_span) =
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcDumpVtable(span) => span)
else {
continue;
};
@ -112,14 +114,14 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
let trait_ref = tcx.impl_trait_ref(def_id).instantiate_identity();
if trait_ref.has_non_region_param() {
tcx.dcx().span_err(
attr.span(),
attr_span,
"`rustc_dump_vtable` must be applied to non-generic impl",
);
continue;
}
if !tcx.is_dyn_compatible(trait_ref.def_id) {
tcx.dcx().span_err(
attr.span(),
attr_span,
"`rustc_dump_vtable` must be applied to dyn-compatible trait",
);
continue;
@ -128,7 +130,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
.try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), trait_ref)
else {
tcx.dcx().span_err(
attr.span(),
attr_span,
"`rustc_dump_vtable` applied to impl header that cannot be normalized",
);
continue;
@ -139,7 +141,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
let ty = tcx.type_of(def_id).instantiate_identity();
if ty.has_non_region_param() {
tcx.dcx().span_err(
attr.span(),
attr_span,
"`rustc_dump_vtable` must be applied to non-generic type",
);
continue;
@ -148,14 +150,13 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
tcx.try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), ty)
else {
tcx.dcx().span_err(
attr.span(),
attr_span,
"`rustc_dump_vtable` applied to type alias that cannot be normalized",
);
continue;
};
let ty::Dynamic(data, _) = *ty.kind() else {
tcx.dcx()
.span_err(attr.span(), "`rustc_dump_vtable` to type alias of dyn type");
tcx.dcx().span_err(attr_span, "`rustc_dump_vtable` to type alias of dyn type");
continue;
};
if let Some(principal) = data.principal() {
@ -168,7 +169,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
}
_ => {
tcx.dcx().span_err(
attr.span(),
attr_span,
"`rustc_dump_vtable` only applies to impl, or type alias of dyn type",
);
continue;

View file

@ -312,6 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcDumpUserArgs
| AttributeKind::RustcDumpItemBounds
| AttributeKind::RustcDumpDefParents
| AttributeKind::RustcDumpVtable(..)
) => { /* do nothing */ }
Attribute::Unparsed(attr_item) => {
style = Some(attr_item.style);
@ -385,7 +386,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| sym::rustc_outlives
| sym::rustc_symbol_name
| sym::rustc_evaluate_where_clauses
| sym::rustc_dump_vtable
| sym::rustc_delayed_bug_from_inside_query
| sym::rustc_def_path
| sym::rustc_partition_reused