Skip to content

Commit e823167

Browse files
committed
Auto merge of #151858 - JonathanBrouwer:rollup-0eagSST, r=JonathanBrouwer
Rollup of 3 pull requests Successful merges: - #151736 (Make some load-from-disk function pointers optional in query vtables) - #151831 (Port `rustc_layout` to attribute parser) - #151856 (Fix flakyness issue with `tests/rustdoc-gui/globals.goml` test)
2 parents 7d8ebe3 + 6d01651 commit e823167

17 files changed

Lines changed: 176 additions & 127 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
2+
use rustc_hir::attrs::RustcLayoutType;
23
use rustc_session::errors;
34

45
use super::prelude::*;
@@ -329,3 +330,61 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcOffloadKernelParser {
329330
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
330331
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOffloadKernel;
331332
}
333+
334+
pub(crate) struct RustcLayoutParser;
335+
336+
impl<S: Stage> CombineAttributeParser<S> for RustcLayoutParser {
337+
const PATH: &[rustc_span::Symbol] = &[sym::rustc_layout];
338+
339+
type Item = RustcLayoutType;
340+
341+
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcLayout(items);
342+
343+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
344+
Allow(Target::Struct),
345+
Allow(Target::Enum),
346+
Allow(Target::Union),
347+
Allow(Target::TyAlias),
348+
]);
349+
350+
const TEMPLATE: AttributeTemplate =
351+
template!(List: &["abi", "align", "size", "homogenous_aggregate", "debug"]);
352+
353+
fn extend(
354+
cx: &mut AcceptContext<'_, '_, S>,
355+
args: &ArgParser,
356+
) -> impl IntoIterator<Item = Self::Item> {
357+
let ArgParser::List(items) = args else {
358+
cx.expected_list(cx.attr_span, args);
359+
return vec![];
360+
};
361+
362+
let mut result = Vec::new();
363+
for item in items.mixed() {
364+
let Some(arg) = item.meta_item() else {
365+
cx.unexpected_literal(item.span());
366+
continue;
367+
};
368+
let Some(ident) = arg.ident() else {
369+
cx.expected_identifier(arg.span());
370+
return vec![];
371+
};
372+
let ty = match ident.name {
373+
sym::abi => RustcLayoutType::Abi,
374+
sym::align => RustcLayoutType::Align,
375+
sym::size => RustcLayoutType::Size,
376+
sym::homogeneous_aggregate => RustcLayoutType::HomogenousAggregate,
377+
sym::debug => RustcLayoutType::Debug,
378+
_ => {
379+
cx.expected_specific_argument(
380+
ident.span,
381+
&[sym::abi, sym::align, sym::size, sym::homogeneous_aggregate, sym::debug],
382+
);
383+
continue;
384+
}
385+
};
386+
result.push(ty);
387+
}
388+
result
389+
}
390+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use crate::attributes::rustc_dump::{
7575
RustcDumpVtable,
7676
};
7777
use crate::attributes::rustc_internal::{
78-
RustcHasIncoherentInherentImplsParser, RustcLayoutScalarValidRangeEndParser,
78+
RustcHasIncoherentInherentImplsParser, RustcLayoutParser, RustcLayoutScalarValidRangeEndParser,
7979
RustcLayoutScalarValidRangeStartParser, RustcLegacyConstGenericsParser,
8080
RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser, RustcLintQueryInstabilityParser,
8181
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
@@ -198,6 +198,7 @@ attribute_parsers!(
198198
Combine<ForceTargetFeatureParser>,
199199
Combine<LinkParser>,
200200
Combine<ReprParser>,
201+
Combine<RustcLayoutParser>,
201202
Combine<TargetFeatureParser>,
202203
Combine<UnstableFeatureBoundParser>,
203204
// tidy-alphabetical-end

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,15 @@ impl IntoDiagArg for CrateType {
690690
}
691691
}
692692

693+
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
694+
pub enum RustcLayoutType {
695+
Abi,
696+
Align,
697+
Size,
698+
HomogenousAggregate,
699+
Debug,
700+
}
701+
693702
/// Represents parsed *built-in* inert attributes.
694703
///
695704
/// ## Overview
@@ -1048,6 +1057,9 @@ pub enum AttributeKind {
10481057
/// Represents `#[rustc_has_incoherent_inherent_impls]`
10491058
RustcHasIncoherentInherentImpls,
10501059

1060+
/// Represents `#[rustc_layout]`
1061+
RustcLayout(ThinVec<RustcLayoutType>),
1062+
10511063
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
10521064
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
10531065

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl AttributeKind {
111111
RustcDumpVtable(..) => No,
112112
RustcDynIncompatibleTrait(..) => No,
113113
RustcHasIncoherentInherentImpls => Yes,
114+
RustcLayout(..) => No,
114115
RustcLayoutScalarValidRangeEnd(..) => Yes,
115116
RustcLayoutScalarValidRangeStart(..) => Yes,
116117
RustcLegacyConstGenerics { .. } => Yes,

compiler/rustc_macros/src/query.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -280,31 +280,21 @@ fn add_query_desc_cached_impl(
280280
let crate::query::Providers { #name: _, .. };
281281
};
282282

283-
// Find out if we should cache the query on disk
284-
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
283+
// Generate a function to check whether we should cache the query to disk, for some key.
284+
if let Some((args, expr)) = modifiers.cache.as_ref() {
285285
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
286286
// expr is a `Block`, meaning that `{ #expr }` gets expanded
287287
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
288288
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
289-
quote! {
289+
cached.extend(quote! {
290290
#[allow(unused_variables, unused_braces, rustc::pass_by_value)]
291291
#[inline]
292292
pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::query::queries::#name::Key<'tcx>) -> bool {
293293
#ra_hint
294294
#expr
295295
}
296-
}
297-
} else {
298-
quote! {
299-
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
300-
#[allow(rustc::pass_by_value)]
301-
#[inline]
302-
pub fn #name<'tcx>(_: TyCtxt<'tcx>, _: &crate::query::queries::#name::Key<'tcx>) -> bool {
303-
#ra_hint
304-
false
305-
}
306-
}
307-
};
296+
});
297+
}
308298

309299
let (tcx, desc) = &modifiers.desc;
310300
let tcx = tcx.as_ref().map_or_else(|| quote! { _ }, |t| quote! { #t });
@@ -322,10 +312,6 @@ fn add_query_desc_cached_impl(
322312
descs.extend(quote! {
323313
#desc
324314
});
325-
326-
cached.extend(quote! {
327-
#cache
328-
});
329315
}
330316

331317
pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ use crate::query::{
1818
};
1919
use crate::ty::TyCtxt;
2020

21+
pub type WillCacheOnDiskForKeyFn<'tcx, Key> = fn(tcx: TyCtxt<'tcx>, key: &Key) -> bool;
22+
23+
pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
24+
tcx: TyCtxt<'tcx>,
25+
key: &Key,
26+
prev_index: SerializedDepNodeIndex,
27+
index: DepNodeIndex,
28+
) -> Option<Value>;
29+
30+
pub type IsLoadableFromDiskFn<'tcx, Key> =
31+
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
32+
2133
/// Stores function pointers and other metadata for a particular query.
2234
///
2335
/// Used indirectly by query plumbing in `rustc_query_system`, via a trait.
@@ -31,18 +43,11 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
3143
pub query_state: usize,
3244
// Offset of this query's cache field in the QueryCaches struct
3345
pub query_cache: usize,
34-
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
46+
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
3547
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
3648
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
37-
pub can_load_from_disk: bool,
38-
pub try_load_from_disk: fn(
39-
tcx: TyCtxt<'tcx>,
40-
key: &C::Key,
41-
prev_index: SerializedDepNodeIndex,
42-
index: DepNodeIndex,
43-
) -> Option<C::Value>,
44-
pub loadable_from_disk:
45-
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
49+
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
50+
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
4651
pub hash_result: HashResult<C::Value>,
4752
pub value_from_cycle_error:
4853
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,6 @@ passes_layout_align =
302302
align: {$align}
303303
passes_layout_homogeneous_aggregate =
304304
homogeneous_aggregate: {$homogeneous_aggregate}
305-
passes_layout_invalid_attribute =
306-
`#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases
307305
passes_layout_of =
308306
layout_of({$normalized_ty}) = {$ty_layout}
309307
passes_layout_size =

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
299299
| AttributeKind::RustcDumpVtable(..)
300300
| AttributeKind::RustcDynIncompatibleTrait(..)
301301
| AttributeKind::RustcHasIncoherentInherentImpls
302+
| AttributeKind::RustcLayout(..)
302303
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
303304
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
304305
| AttributeKind::RustcLintOptDenyFieldAccess { .. }

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,6 @@ pub(crate) struct LayoutOf<'tcx> {
519519
pub ty_layout: String,
520520
}
521521

522-
#[derive(Diagnostic)]
523-
#[diag(passes_layout_invalid_attribute)]
524-
pub(crate) struct LayoutInvalidAttribute {
525-
#[primary_span]
526-
pub span: Span,
527-
}
528-
529522
#[derive(Diagnostic)]
530523
#[diag(passes_abi_of)]
531524
pub(crate) struct AbiOf {

compiler/rustc_passes/src/layout_test.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
use rustc_abi::{HasDataLayout, TargetDataLayout};
2-
use rustc_hir::Attribute;
2+
use rustc_hir::attrs::{AttributeKind, RustcLayoutType};
33
use rustc_hir::def::DefKind;
44
use rustc_hir::def_id::LocalDefId;
5+
use rustc_hir::find_attr;
56
use rustc_middle::span_bug;
67
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers};
78
use rustc_middle::ty::{self, Ty, TyCtxt};
9+
use rustc_span::Span;
810
use rustc_span::source_map::Spanned;
9-
use rustc_span::{Span, sym};
1011
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
1112
use rustc_trait_selection::infer::TyCtxtInferExt;
1213
use rustc_trait_selection::traits;
1314

14-
use crate::errors::{
15-
LayoutAbi, LayoutAlign, LayoutHomogeneousAggregate, LayoutInvalidAttribute, LayoutOf,
16-
LayoutSize, UnrecognizedArgument,
17-
};
15+
use crate::errors::{LayoutAbi, LayoutAlign, LayoutHomogeneousAggregate, LayoutOf, LayoutSize};
1816

1917
pub fn test_layout(tcx: TyCtxt<'_>) {
2018
if !tcx.features().rustc_attrs() {
2119
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
2220
return;
2321
}
2422
for id in tcx.hir_crate_items(()).definitions() {
25-
for attr in tcx.get_attrs(id, sym::rustc_layout) {
26-
match tcx.def_kind(id) {
27-
DefKind::TyAlias | DefKind::Enum | DefKind::Struct | DefKind::Union => {
28-
dump_layout_of(tcx, id, attr);
29-
}
30-
_ => {
31-
tcx.dcx().emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
32-
}
23+
let attrs = tcx.get_all_attrs(id);
24+
if let Some(attrs) = find_attr!(attrs, AttributeKind::RustcLayout(attrs) => attrs) {
25+
// Attribute parsing handles error reporting
26+
if matches!(
27+
tcx.def_kind(id),
28+
DefKind::TyAlias | DefKind::Enum | DefKind::Struct | DefKind::Union
29+
) {
30+
dump_layout_of(tcx, id, attrs);
3331
}
3432
}
3533
}
@@ -66,7 +64,7 @@ pub fn ensure_wf<'tcx>(
6664
}
6765
}
6866

69-
fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
67+
fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attrs: &[RustcLayoutType]) {
7068
let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
7169
let ty = tcx.type_of(item_def_id).instantiate_identity();
7270
let span = tcx.def_span(item_def_id.to_def_id());
@@ -75,32 +73,29 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
7573
}
7674
match tcx.layout_of(typing_env.as_query_input(ty)) {
7775
Ok(ty_layout) => {
78-
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
79-
// The `..` are the names of fields to dump.
80-
let meta_items = attr.meta_item_list().unwrap_or_default();
81-
for meta_item in meta_items {
82-
match meta_item.name() {
76+
for attr in attrs {
77+
match attr {
8378
// FIXME: this never was about ABI and now this dump arg is confusing
84-
Some(sym::abi) => {
79+
RustcLayoutType::Abi => {
8580
tcx.dcx().emit_err(LayoutAbi {
8681
span,
8782
abi: format!("{:?}", ty_layout.backend_repr),
8883
});
8984
}
9085

91-
Some(sym::align) => {
86+
RustcLayoutType::Align => {
9287
tcx.dcx().emit_err(LayoutAlign {
9388
span,
9489
align: format!("{:?}", ty_layout.align),
9590
});
9691
}
9792

98-
Some(sym::size) => {
93+
RustcLayoutType::Size => {
9994
tcx.dcx()
10095
.emit_err(LayoutSize { span, size: format!("{:?}", ty_layout.size) });
10196
}
10297

103-
Some(sym::homogeneous_aggregate) => {
98+
RustcLayoutType::HomogenousAggregate => {
10499
tcx.dcx().emit_err(LayoutHomogeneousAggregate {
105100
span,
106101
homogeneous_aggregate: format!(
@@ -111,16 +106,12 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
111106
});
112107
}
113108

114-
Some(sym::debug) => {
109+
RustcLayoutType::Debug => {
115110
let normalized_ty = tcx.normalize_erasing_regions(typing_env, ty);
116111
// FIXME: using the `Debug` impl here isn't ideal.
117112
let ty_layout = format!("{:#?}", *ty_layout);
118113
tcx.dcx().emit_err(LayoutOf { span, normalized_ty, ty_layout });
119114
}
120-
121-
_ => {
122-
tcx.dcx().emit_err(UnrecognizedArgument { span: meta_item.span() });
123-
}
124115
}
125116
}
126117
}

0 commit comments

Comments
 (0)