Skip to content

Commit 8b18a20

Browse files
authored
Unrolled build for #142491
Rollup merge of #142491 - jdonszelmann:cold, r=oli-obk Rework #[cold] attribute parser r? `@oli-obk`
2 parents 5526a2f + b9107a8 commit 8b18a20

File tree

12 files changed

+99
-35
lines changed

12 files changed

+99
-35
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ pub enum AttributeKind {
202202
span: Span,
203203
},
204204

205+
/// Represents `#[cold]`.
206+
Cold(Span),
207+
205208
/// Represents `#[rustc_confusables]`.
206209
Confusables {
207210
symbols: ThinVec<Symbol>,

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
3838
Some(AttributeKind::Optimize(res, cx.attr_span))
3939
}
4040
}
41+
42+
pub(crate) struct ColdParser;
43+
44+
impl<S: Stage> SingleAttributeParser<S> for ColdParser {
45+
const PATH: &[rustc_span::Symbol] = &[sym::cold];
46+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
47+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
48+
const TEMPLATE: AttributeTemplate = template!(Word);
49+
50+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
51+
if !args.no_args() {
52+
cx.expected_no_args(args.span().unwrap_or(cx.attr_span));
53+
return None;
54+
};
55+
56+
Some(AttributeKind::Cold(cx.attr_span))
57+
}
58+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_session::Session;
1515
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616

1717
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
18-
use crate::attributes::codegen_attrs::OptimizeParser;
18+
use crate::attributes::codegen_attrs::{ColdParser, OptimizeParser};
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
2121
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -106,6 +106,7 @@ attribute_parsers!(
106106

107107
// tidy-alphabetical-start
108108
Single<AsPtrParser>,
109+
Single<ColdParser>,
109110
Single<ConstStabilityIndirectParser>,
110111
Single<DeprecationParser>,
111112
Single<InlineParser>,
@@ -234,6 +235,16 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
234235
})
235236
}
236237

238+
pub(crate) fn expected_no_args(&self, args_span: Span) -> ErrorGuaranteed {
239+
self.emit_err(AttributeParseError {
240+
span: args_span,
241+
attr_span: self.attr_span,
242+
template: self.template.clone(),
243+
attribute: self.attr_path.clone(),
244+
reason: AttributeParseErrorReason::ExpectedNoArgs,
245+
})
246+
}
247+
237248
/// emit an error that a `name = value` pair was expected at this span. The symbol can be given for
238249
/// a nicer error message talking about the specific name that was found lacking a value.
239250
pub(crate) fn expected_name_value(&self, span: Span, name: Option<Symbol>) -> ErrorGuaranteed {

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ pub(crate) struct UnrecognizedReprHint {
474474
}
475475

476476
pub(crate) enum AttributeParseErrorReason {
477+
ExpectedNoArgs,
477478
ExpectedStringLiteral { byte_string: Option<Span> },
478479
ExpectedSingleArgument,
479480
ExpectedList,
@@ -529,6 +530,10 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
529530
diag.span_label(self.span, format!("didn't expect a literal here"));
530531
diag.code(E0565);
531532
}
533+
AttributeParseErrorReason::ExpectedNoArgs => {
534+
diag.span_label(self.span, format!("didn't expect any arguments here"));
535+
diag.code(E0565);
536+
}
532537
AttributeParseErrorReason::ExpectedNameValue(None) => {
533538
diag.span_label(
534539
self.span,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_abi::ExternAbi;
44
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
55
use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
66
use rustc_attr_data_structures::{
7-
AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, find_attr,
7+
AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, ReprAttr, find_attr,
88
};
99
use rustc_hir::def::DefKind;
1010
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
@@ -110,16 +110,27 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
110110
}
111111
};
112112

113-
if let hir::Attribute::Parsed(AttributeKind::Align { align, .. }) = attr {
114-
codegen_fn_attrs.alignment = Some(*align);
113+
if let hir::Attribute::Parsed(p) = attr {
114+
match p {
115+
AttributeKind::Repr(reprs) => {
116+
codegen_fn_attrs.alignment = reprs
117+
.iter()
118+
.filter_map(
119+
|(r, _)| if let ReprAttr::ReprAlign(x) = r { Some(*x) } else { None },
120+
)
121+
.max();
122+
}
123+
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
124+
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
125+
_ => {}
126+
}
115127
}
116128

117129
let Some(Ident { name, .. }) = attr.ident() else {
118130
continue;
119131
};
120132

121133
match name {
122-
sym::cold => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
123134
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
124135
sym::ffi_pure => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
125136
sym::ffi_const => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ fn emit_malformed_attribute(
292292
| sym::align
293293
| sym::deprecated
294294
| sym::optimize
295+
| sym::cold
295296
) {
296297
return;
297298
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
149149
}
150150
Attribute::Parsed(AttributeKind::Repr(_)) => { /* handled below this loop and elsewhere */
151151
}
152+
Attribute::Parsed(AttributeKind::Cold(attr_span)) => {
153+
self.check_cold(hir_id, *attr_span, span, target)
154+
}
152155
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
153156
self.check_align(span, target, *align, *repr_span)
154157
}
155-
156158
Attribute::Parsed(
157159
AttributeKind::BodyStability { .. }
158160
| AttributeKind::ConstStabilityIndirect
@@ -245,7 +247,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
245247
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
246248
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
247249
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
248-
[sym::cold, ..] => self.check_cold(hir_id, attr, span, target),
249250
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
250251
[sym::link_name, ..] => self.check_link_name(hir_id, attr, span, target),
251252
[sym::link_section, ..] => self.check_link_section(hir_id, attr, span, target),
@@ -651,8 +652,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
651652
sym::repr,
652653
sym::align,
653654
sym::rustc_std_internal_symbol,
654-
// code generation
655-
sym::cold,
656655
// documentation
657656
sym::doc,
658657
];
@@ -688,7 +687,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
688687
Attribute::Parsed(
689688
AttributeKind::Deprecation { .. }
690689
| AttributeKind::Repr { .. }
691-
| AttributeKind::Align { .. },
690+
| AttributeKind::Align { .. }
691+
| AttributeKind::Cold(..),
692692
) => {
693693
continue;
694694
}
@@ -1640,23 +1640,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16401640
}
16411641

16421642
/// Checks if `#[cold]` is applied to a non-function.
1643-
fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1643+
fn check_cold(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
16441644
match target {
16451645
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {}
16461646
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
16471647
// `#[cold]` attribute with just a lint, because we previously
16481648
// erroneously allowed it and some crates used it accidentally, to be compatible
16491649
// with crates depending on them, we can't throw an error here.
16501650
Target::Field | Target::Arm | Target::MacroDef => {
1651-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "cold");
1651+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "cold");
16521652
}
16531653
_ => {
16541654
// FIXME: #[cold] was previously allowed on non-functions and some crates used
16551655
// this, so only emit a warning.
16561656
self.tcx.emit_node_span_lint(
16571657
UNUSED_ATTRIBUTES,
16581658
hir_id,
1659-
attr.span(),
1659+
attr_span,
16601660
errors::Cold { span, on_crate: hir_id == CRATE_HIR_ID },
16611661
);
16621662
}

src/rustdoc-json-types/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3737
// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line
3838
// are deliberately not in a doc comment, because they need not be in public docs.)
3939
//
40-
// Latest feature: Pretty printing of optimize attributes changed
41-
pub const FORMAT_VERSION: u32 = 49;
40+
// Latest feature: Pretty printing of cold attributes changed
41+
pub const FORMAT_VERSION: u32 = 50;
4242

4343
/// The root of the emitted JSON blob.
4444
///

tests/ui/attributes/expected-word.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[cold = true]
2+
//~^ ERROR malformed `cold` attribute input [E0565]
3+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0565]: malformed `cold` attribute input
2+
--> $DIR/expected-word.rs:1:1
3+
|
4+
LL | #[cold = true]
5+
| ^^^^^^^------^
6+
| | |
7+
| | didn't expect any arguments here
8+
| help: must be of the form: `#[cold]`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0565`.

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,6 @@ warning: `#[proc_macro_derive]` only has an effect on functions
379379
LL | #![proc_macro_derive()]
380380
| ^^^^^^^^^^^^^^^^^^^^^^^
381381

382-
warning: attribute should be applied to a function definition
383-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:62:1
384-
|
385-
LL | #![cold]
386-
| ^^^^^^^^ cannot be applied to crates
387-
|
388-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
389-
390382
warning: attribute should be applied to an `extern` block with non-Rust ABI
391383
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:64:1
392384
|
@@ -417,6 +409,14 @@ warning: `#[must_use]` has no effect when applied to a module
417409
LL | #![must_use]
418410
| ^^^^^^^^^^^^
419411

412+
warning: attribute should be applied to a function definition
413+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:62:1
414+
|
415+
LL | #![cold]
416+
| ^^^^^^^^ cannot be applied to crates
417+
|
418+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
419+
420420
warning: `#[macro_use]` only has an effect on `extern crate` and modules
421421
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:176:5
422422
|

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ note: attribute also specified here
102102
LL | #[automatically_derived]
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^
104104

105-
error: unused attribute
106-
--> $DIR/unused-attr-duplicate.rs:77:1
107-
|
108-
LL | #[cold]
109-
| ^^^^^^^ help: remove this attribute
110-
|
111-
note: attribute also specified here
112-
--> $DIR/unused-attr-duplicate.rs:76:1
113-
|
114-
LL | #[cold]
115-
| ^^^^^^^
116-
117105
error: unused attribute
118106
--> $DIR/unused-attr-duplicate.rs:79:1
119107
|
@@ -289,5 +277,17 @@ LL | #[inline(always)]
289277
| ^^^^^^^^^^^^^^^^^
290278
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
291279

280+
error: unused attribute
281+
--> $DIR/unused-attr-duplicate.rs:77:1
282+
|
283+
LL | #[cold]
284+
| ^^^^^^^ help: remove this attribute
285+
|
286+
note: attribute also specified here
287+
--> $DIR/unused-attr-duplicate.rs:76:1
288+
|
289+
LL | #[cold]
290+
| ^^^^^^^
291+
292292
error: aborting due to 23 previous errors
293293

0 commit comments

Comments
 (0)