Skip to content

Commit b755b3b

Browse files
committed
Port #![no_builtins] to the attribute parser.
1 parent 9af7dca commit b755b3b

11 files changed

Lines changed: 114 additions & 89 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,12 @@ impl<S: Stage> NoArgsAttributeParser<S> for ProfilerRuntimeParser {
211211
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
212212
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ProfilerRuntime;
213213
}
214+
215+
pub(crate) struct NoBuiltinsParser;
216+
217+
impl<S: Stage> NoArgsAttributeParser<S> for NoBuiltinsParser {
218+
const PATH: &[Symbol] = &[sym::no_builtins];
219+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
220+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
221+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoBuiltins;
222+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use crate::attributes::codegen_attrs::{
2828
};
2929
use crate::attributes::confusables::ConfusablesParser;
3030
use crate::attributes::crate_level::{
31-
CrateNameParser, MoveSizeLimitParser, NeedsPanicRuntimeParser, NoCoreParser, NoMainParser, NoStdParser,
32-
PanicRuntimeParser, PatternComplexityLimitParser, ProfilerRuntimeParser, RecursionLimitParser,
33-
RustcCoherenceIsCoreParser, TypeLengthLimitParser, WindowsSubsystemParser,
31+
CrateNameParser, MoveSizeLimitParser, NeedsPanicRuntimeParser, NoBuiltinsParser, NoCoreParser,
32+
NoMainParser, NoStdParser, PanicRuntimeParser, PatternComplexityLimitParser,
33+
ProfilerRuntimeParser, RecursionLimitParser, RustcCoherenceIsCoreParser, TypeLengthLimitParser,
34+
WindowsSubsystemParser,
3435
};
3536
use crate::attributes::debugger::DebuggerViualizerParser;
3637
use crate::attributes::deprecation::DeprecationParser;
@@ -268,6 +269,7 @@ attribute_parsers!(
268269
Single<WithoutArgs<MayDangleParser>>,
269270
Single<WithoutArgs<NeedsAllocatorParser>>,
270271
Single<WithoutArgs<NeedsPanicRuntimeParser>>,
272+
Single<WithoutArgs<NoBuiltinsParser>>,
271273
Single<WithoutArgs<NoCoreParser>>,
272274
Single<WithoutArgs<NoImplicitPreludeParser>>,
273275
Single<WithoutArgs<NoLinkParser>>,

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::sync::mpsc::{Receiver, Sender, channel};
77
use std::{fs, io, mem, str, thread};
88

99
use rustc_abi::Size;
10-
use rustc_ast::attr;
1110
use rustc_data_structures::fx::FxIndexMap;
1211
use rustc_data_structures::jobserver::{self, Acquired};
1312
use rustc_data_structures::memmap::Mmap;
@@ -19,6 +18,8 @@ use rustc_errors::{
1918
MultiSpan, Style, Suggestions,
2019
};
2120
use rustc_fs_util::link_or_copy;
21+
use rustc_hir::attrs::AttributeKind;
22+
use rustc_hir::find_attr;
2223
use rustc_incremental::{
2324
copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,
2425
};
@@ -31,7 +32,7 @@ use rustc_session::config::{
3132
self, CrateType, Lto, OutFileName, OutputFilenames, OutputType, Passes, SwitchWithOptPath,
3233
};
3334
use rustc_span::source_map::SourceMap;
34-
use rustc_span::{FileName, InnerSpan, Span, SpanData, sym};
35+
use rustc_span::{FileName, InnerSpan, Span, SpanData};
3536
use rustc_target::spec::{MergeFunctions, SanitizerSet};
3637
use tracing::debug;
3738

@@ -435,7 +436,7 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
435436
let (coordinator_send, coordinator_receive) = channel();
436437

437438
let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
438-
let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
439+
let no_builtins = find_attr!(crate_attrs, AttributeKind::NoBuiltins);
439440

440441
let crate_info = CrateInfo::new(tcx, target_cpu);
441442

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::str::FromStr;
22

33
use rustc_abi::{Align, ExternAbi};
44
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
5-
use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
5+
use rustc_ast::{LitKind, MetaItem, MetaItemInner};
66
use rustc_hir::attrs::{
77
AttributeKind, EiiImplResolution, InlineAttr, Linkage, RtsanSetting, UsedBy,
88
};
@@ -419,7 +419,7 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
419419
// When `no_builtins` is applied at the crate level, we should add the
420420
// `no-builtins` attribute to each function to ensure it takes effect in LTO.
421421
let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
422-
let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
422+
let no_builtins = find_attr!(crate_attrs, AttributeKind::NoBuiltins);
423423
if no_builtins {
424424
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
425425
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ pub enum AttributeKind {
849849
/// Represents `#[needs_panic_runtime]`
850850
NeedsPanicRuntime,
851851

852+
/// Represents `#[no_builtins]`
853+
NoBuiltins,
854+
852855
/// Represents `#[no_core]`
853856
NoCore(Span),
854857

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl AttributeKind {
7878
Naked(..) => No,
7979
NeedsAllocator => No,
8080
NeedsPanicRuntime => No,
81+
NoBuiltins => Yes,
8182
NoCore(..) => No,
8283
NoImplicitPrelude(..) => No,
8384
NoLink => No,

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
744744
compiler_builtins: find_attr!(attrs, AttributeKind::CompilerBuiltins),
745745
needs_allocator: find_attr!(attrs, AttributeKind::NeedsAllocator),
746746
needs_panic_runtime: find_attr!(attrs, AttributeKind::NeedsPanicRuntime),
747-
no_builtins: ast::attr::contains_name(attrs, sym::no_builtins),
747+
no_builtins: find_attr!(attrs, AttributeKind::NoBuiltins),
748748
panic_runtime: find_attr!(attrs, AttributeKind::PanicRuntime),
749749
profiler_runtime: find_attr!(attrs, AttributeKind::ProfilerRuntime),
750750
symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(),

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
326326
| AttributeKind::PanicRuntime
327327
| AttributeKind::NeedsPanicRuntime
328328
| AttributeKind::ProfilerRuntime
329+
| AttributeKind::NoBuiltins
329330
) => { /* do nothing */ }
330331
Attribute::Unparsed(attr_item) => {
331332
style = Some(attr_item.style);
@@ -401,7 +402,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
401402
| sym::rustc_no_implicit_bounds
402403
| sym::test_runner
403404
| sym::reexport_test_harness_main
404-
| sym::no_builtins
405405
| sym::crate_type
406406
| sym::rustc_preserve_ub_checks,
407407
..

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,26 +908,26 @@ mod no_main_1 {
908908

909909
#[no_builtins]
910910
//~^ WARN crate-level attribute should be an inner attribute
911-
//~| HELP add a `!`
912911
mod no_builtins {
912+
//~^ NOTE: this attribute does not have an `!`, which means it is applied to this module
913913
mod inner { #![no_builtins] }
914-
//~^ WARN crate-level attribute should be in the root module
914+
//~^ WARN the `#![no_builtins]` attribute can only be used at the crate root
915915

916916
#[no_builtins] fn f() { }
917917
//~^ WARN crate-level attribute should be an inner attribute
918-
//~| HELP add a `!`
918+
//~| NOTE this attribute does not have an `!`, which means it is applied to this function
919919

920920
#[no_builtins] struct S;
921921
//~^ WARN crate-level attribute should be an inner attribute
922-
//~| HELP add a `!`
922+
//~| NOTE this attribute does not have an `!`, which means it is applied to this struct
923923

924924
#[no_builtins] type T = S;
925925
//~^ WARN crate-level attribute should be an inner attribute
926-
//~| HELP add a `!`
926+
//~| NOTE this attribute does not have an `!`, which means it is applied to this type alias
927927

928928
#[no_builtins] impl S { }
929929
//~^ WARN crate-level attribute should be an inner attribute
930-
//~| HELP add a `!`
930+
//~| NOTE this attribute does not have an `!`, which means it is applied to this implementation
931931
}
932932

933933
#[recursion_limit="0200"]

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

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,6 @@ help: add a `!`
240240
LL | #![feature(x0600)]
241241
| +
242242

243-
warning: crate-level attribute should be an inner attribute
244-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:909:1
245-
|
246-
LL | #[no_builtins]
247-
| ^^^^^^^^^^^^^^
248-
|
249-
help: add a `!`
250-
|
251-
LL | #![no_builtins]
252-
| +
253-
254243
warning: attribute should be applied to an `extern` block with non-Rust ABI
255244
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:71:1
256245
|
@@ -465,56 +454,6 @@ help: add a `!`
465454
LL | #![feature(x0600)] impl S { }
466455
| +
467456

468-
warning: crate-level attribute should be in the root module
469-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:913:17
470-
|
471-
LL | mod inner { #![no_builtins] }
472-
| ^^^^^^^^^^^^^^^
473-
474-
warning: crate-level attribute should be an inner attribute
475-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:916:5
476-
|
477-
LL | #[no_builtins] fn f() { }
478-
| ^^^^^^^^^^^^^^
479-
|
480-
help: add a `!`
481-
|
482-
LL | #![no_builtins] fn f() { }
483-
| +
484-
485-
warning: crate-level attribute should be an inner attribute
486-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:920:5
487-
|
488-
LL | #[no_builtins] struct S;
489-
| ^^^^^^^^^^^^^^
490-
|
491-
help: add a `!`
492-
|
493-
LL | #![no_builtins] struct S;
494-
| +
495-
496-
warning: crate-level attribute should be an inner attribute
497-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:924:5
498-
|
499-
LL | #[no_builtins] type T = S;
500-
| ^^^^^^^^^^^^^^
501-
|
502-
help: add a `!`
503-
|
504-
LL | #![no_builtins] type T = S;
505-
| +
506-
507-
warning: crate-level attribute should be an inner attribute
508-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:928:5
509-
|
510-
LL | #[no_builtins] impl S { }
511-
| ^^^^^^^^^^^^^^
512-
|
513-
help: add a `!`
514-
|
515-
LL | #![no_builtins] impl S { }
516-
| +
517-
518457
warning: `#[macro_use]` attribute cannot be used on functions
519458
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:190:5
520459
|
@@ -1416,6 +1355,76 @@ note: this attribute does not have an `!`, which means it is applied to this imp
14161355
LL | #[no_main] impl S { }
14171356
| ^^^^^^^^^^
14181357

1358+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_builtins]`
1359+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:909:1
1360+
|
1361+
LL | #[no_builtins]
1362+
| ^^^^^^^^^^^^^^
1363+
|
1364+
note: this attribute does not have an `!`, which means it is applied to this module
1365+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:911:1
1366+
|
1367+
LL | / mod no_builtins {
1368+
LL | |
1369+
LL | | mod inner { #![no_builtins] }
1370+
... |
1371+
LL | | }
1372+
| |_^
1373+
1374+
warning: the `#![no_builtins]` attribute can only be used at the crate root
1375+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:913:17
1376+
|
1377+
LL | mod inner { #![no_builtins] }
1378+
| ^^^^^^^^^^^^^^^
1379+
1380+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_builtins]`
1381+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:916:5
1382+
|
1383+
LL | #[no_builtins] fn f() { }
1384+
| ^^^^^^^^^^^^^^
1385+
|
1386+
note: this attribute does not have an `!`, which means it is applied to this function
1387+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:916:20
1388+
|
1389+
LL | #[no_builtins] fn f() { }
1390+
| ^^^^^^^^^^
1391+
1392+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_builtins]`
1393+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:920:5
1394+
|
1395+
LL | #[no_builtins] struct S;
1396+
| ^^^^^^^^^^^^^^
1397+
|
1398+
note: this attribute does not have an `!`, which means it is applied to this struct
1399+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:920:20
1400+
|
1401+
LL | #[no_builtins] struct S;
1402+
| ^^^^^^^^^
1403+
1404+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_builtins]`
1405+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:924:5
1406+
|
1407+
LL | #[no_builtins] type T = S;
1408+
| ^^^^^^^^^^^^^^
1409+
|
1410+
note: this attribute does not have an `!`, which means it is applied to this type alias
1411+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:924:20
1412+
|
1413+
LL | #[no_builtins] type T = S;
1414+
| ^^^^^^^^^^^
1415+
1416+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_builtins]`
1417+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:928:5
1418+
|
1419+
LL | #[no_builtins] impl S { }
1420+
| ^^^^^^^^^^^^^^
1421+
|
1422+
note: this attribute does not have an `!`, which means it is applied to this implementation block
1423+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:928:20
1424+
|
1425+
LL | #[no_builtins] impl S { }
1426+
| ^^^^^^^^^^
1427+
14191428
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![recursion_limit]`
14201429
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:933:1
14211430
|

0 commit comments

Comments
 (0)