Skip to content

Commit e259f68

Browse files
committed
cold
1 parent 9b3c761 commit e259f68

File tree

8 files changed

+81
-33
lines changed

8 files changed

+81
-33
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(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, span: Span) -> ErrorGuaranteed {
239+
self.emit_err(AttributeParseError {
240+
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_data_structures::fx::FxHashMap;
1010
use rustc_hir::def::DefKind;
@@ -109,16 +109,27 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
109109
}
110110
};
111111

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

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

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

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
}
@@ -1637,23 +1637,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16371637
}
16381638

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

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)