Skip to content

Commit d4efab1

Browse files
committed
cold
1 parent b64fd13 commit d4efab1

File tree

8 files changed

+68
-29
lines changed

8 files changed

+68
-29
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ pub enum AttributeKind {
199199
span: Span,
200200
},
201201

202+
/// Represents `#[cold]`.
203+
Cold(Span),
204+
202205
/// Represents `#[rustc_confusables]`.
203206
Confusables {
204207
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};
@@ -105,6 +105,7 @@ attribute_parsers!(
105105

106106
// tidy-alphabetical-start
107107
Single<AsPtrParser>,
108+
Single<ColdParser>,
108109
Single<ConstStabilityIndirectParser>,
109110
Single<DeprecationParser>,
110111
Single<InlineParser>,
@@ -233,6 +234,16 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
233234
})
234235
}
235236

237+
pub(crate) fn expected_no_args(&self, span: Span) -> ErrorGuaranteed {
238+
self.emit_err(AttributeParseError {
239+
span,
240+
attr_span: self.attr_span,
241+
template: self.template.clone(),
242+
attribute: self.attr_path.clone(),
243+
reason: AttributeParseErrorReason::ExpectedNoArgs,
244+
})
245+
}
246+
236247
/// emit an error that a `name = value` pair was expected at this span. The symbol can be given for
237248
/// a nicer error message talking about the specific name that was found lacking a value.
238249
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
@@ -466,6 +466,7 @@ pub(crate) struct UnrecognizedReprHint {
466466
}
467467

468468
pub(crate) enum AttributeParseErrorReason {
469+
ExpectedNoArgs,
469470
ExpectedStringLiteral { byte_string: Option<Span> },
470471
ExpectedSingleArgument,
471472
ExpectedList,
@@ -521,6 +522,10 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
521522
diag.span_label(self.span, format!("didn't expect a literal here"));
522523
diag.code(E0565);
523524
}
525+
AttributeParseErrorReason::ExpectedNoArgs => {
526+
diag.span_label(self.span, format!("didn't expect any arguments here"));
527+
diag.code(E0565);
528+
}
524529
AttributeParseErrorReason::ExpectedNameValue(None) => {
525530
diag.span_label(
526531
self.span,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
118118
.filter_map(|(r, _)| if let ReprAlign(x) = r { Some(*x) } else { None })
119119
.max();
120120
}
121+
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
121122

122123
_ => {}
123124
}
@@ -128,7 +129,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
128129
};
129130

130131
match name {
131-
sym::cold => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
132132
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
133133
sym::ffi_pure => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
134134
sym::ffi_const => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ 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(
153156
AttributeKind::BodyStability { .. }
154157
| AttributeKind::ConstStabilityIndirect
@@ -241,7 +244,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
241244
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
242245
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
243246
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
244-
[sym::cold, ..] => self.check_cold(hir_id, attr, span, target),
245247
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
246248
[sym::link_name, ..] => self.check_link_name(hir_id, attr, span, target),
247249
[sym::link_section, ..] => self.check_link_section(hir_id, attr, span, target),
@@ -646,8 +648,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
646648
sym::instruction_set,
647649
sym::repr,
648650
sym::rustc_std_internal_symbol,
649-
// code generation
650-
sym::cold,
651651
// documentation
652652
sym::doc,
653653
];
@@ -681,7 +681,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
681681
// this check can be part of the parser and be removed here
682682
match other_attr {
683683
Attribute::Parsed(
684-
AttributeKind::Deprecation { .. } | AttributeKind::Repr { .. },
684+
AttributeKind::Deprecation { .. }
685+
| AttributeKind::Repr { .. }
686+
| AttributeKind::Cold(..),
685687
) => {
686688
continue;
687689
}
@@ -1630,23 +1632,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16301632
}
16311633

16321634
/// Checks if `#[cold]` is applied to a non-function.
1633-
fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1635+
fn check_cold(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
16341636
match target {
16351637
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {}
16361638
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
16371639
// `#[cold]` attribute with just a lint, because we previously
16381640
// erroneously allowed it and some crates used it accidentally, to be compatible
16391641
// with crates depending on them, we can't throw an error here.
16401642
Target::Field | Target::Arm | Target::MacroDef => {
1641-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "cold");
1643+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "cold");
16421644
}
16431645
_ => {
16441646
// FIXME: #[cold] was previously allowed on non-functions and some crates used
16451647
// this, so only emit a warning.
16461648
self.tcx.emit_node_span_lint(
16471649
UNUSED_ATTRIBUTES,
16481650
hir_id,
1649-
attr.span(),
1651+
attr_span,
16501652
errors::Cold { span, on_crate: hir_id == CRATE_HIR_ID },
16511653
);
16521654
}

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)