File tree 8 files changed +96
-6
lines changed
8 files changed +96
-6
lines changed Original file line number Diff line number Diff line change @@ -172,6 +172,8 @@ impl CodegenFnAttrs {
172
172
/// * `#[no_mangle]` is present
173
173
/// * `#[export_name(...)]` is present
174
174
/// * `#[linkage]` is present
175
+ ///
176
+ /// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
175
177
pub fn contains_extern_indicator ( & self ) -> bool {
176
178
self . flags . contains ( CodegenFnAttrFlags :: NO_MANGLE )
177
179
|| self . flags . contains ( CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL )
Original file line number Diff line number Diff line change @@ -150,6 +150,7 @@ impl<'tcx> MonoItem<'tcx> {
150
150
151
151
// If the function is #[naked] or contains any other attribute that requires exactly-once
152
152
// instantiation:
153
+ // We emit an unused_attributes lint for this case, which should be kept in sync if possible.
153
154
let codegen_fn_attrs = tcx. codegen_fn_attrs ( instance. def_id ( ) ) ;
154
155
if codegen_fn_attrs. contains_extern_indicator ( )
155
156
|| codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: NAKED )
Original file line number Diff line number Diff line change @@ -383,6 +383,10 @@ passes_inline_ignored_constants =
383
383
.warn = { -passes_previously_accepted }
384
384
.note = { -passes_see_issue (issue : " 65833 " )}
385
385
386
+ passes_inline_ignored_for_exported =
387
+ `#[inline]` is ignored on externally exported functions
388
+ .help = externally exported functions are functions with `#[no_mangle]`, `#[export_name]`, or `#[linkage]`
389
+
386
390
passes_inline_ignored_function_prototype =
387
391
`#[inline]` is ignored on function prototypes
388
392
Original file line number Diff line number Diff line change @@ -451,6 +451,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
451
451
} ) ;
452
452
}
453
453
}
454
+
455
+ // `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
456
+ if let Some ( did) = hir_id. as_owner ( )
457
+ && self . tcx . def_kind ( did) . has_codegen_attrs ( )
458
+ && !matches ! ( attr. meta_item_list( ) . as_deref( ) , Some ( [ item] ) if item. has_name( sym:: never) )
459
+ {
460
+ let attrs = self . tcx . codegen_fn_attrs ( did) ;
461
+ // Not checking naked as `#[inline]` is forbidden for naked functions anyways.
462
+ if attrs. contains_extern_indicator ( ) {
463
+ self . tcx . emit_node_span_lint (
464
+ UNUSED_ATTRIBUTES ,
465
+ hir_id,
466
+ attr. span ( ) ,
467
+ errors:: InlineIgnoredForExported { } ,
468
+ ) ;
469
+ }
470
+ }
454
471
}
455
472
456
473
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,
Original file line number Diff line number Diff line change @@ -1441,6 +1441,11 @@ pub(crate) struct OnlyHasEffectOn {
1441
1441
pub target_name : String ,
1442
1442
}
1443
1443
1444
+ #[ derive( LintDiagnostic ) ]
1445
+ #[ diag( passes_inline_ignored_for_exported) ]
1446
+ #[ help]
1447
+ pub ( crate ) struct InlineIgnoredForExported { }
1448
+
1444
1449
#[ derive( Diagnostic ) ]
1445
1450
#[ diag( passes_object_lifetime_err) ]
1446
1451
pub ( crate ) struct ObjectLifetimeErr {
Original file line number Diff line number Diff line change
1
+ //! Ensure the unused_attributes lint fires for externally exported functions with `#[inline]`,
2
+ //! because `#[inline]` is ignored for such functions.
3
+
4
+ #![ crate_type = "lib" ]
5
+
6
+ #![ feature( linkage) ]
7
+ #![ feature( naked_functions) ]
8
+ #![ deny( unused_attributes) ]
9
+
10
+ #[ inline]
11
+ //~^ ERROR: `#[inline]` is ignored on externally exported functions
12
+ #[ no_mangle]
13
+ fn no_mangle ( ) { }
14
+
15
+ #[ inline]
16
+ //~^ ERROR: `#[inline]` is ignored on externally exported functions
17
+ #[ export_name = "export_name" ]
18
+ fn export_name ( ) { }
19
+
20
+ #[ inline]
21
+ //~^ ERROR: `#[inline]` is ignored on externally exported functions
22
+ #[ linkage = "external" ]
23
+ fn external_linkage ( ) { }
24
+
25
+ #[ inline]
26
+ fn normal ( ) { }
27
+
28
+ #[ inline]
29
+ #[ linkage = "internal" ] // not exported
30
+ fn internal_linkage ( ) { }
Original file line number Diff line number Diff line change
1
+ error: `#[inline]` is ignored on externally exported functions
2
+ --> $DIR/inline-exported.rs:10:1
3
+ |
4
+ LL | #[inline]
5
+ | ^^^^^^^^^
6
+ |
7
+ = help: externally exported functions are functions with `#[no_mangle]`, `#[export_name]`, or `#[linkage]`
8
+ note: the lint level is defined here
9
+ --> $DIR/inline-exported.rs:8:9
10
+ |
11
+ LL | #![deny(unused_attributes)]
12
+ | ^^^^^^^^^^^^^^^^^
13
+
14
+ error: `#[inline]` is ignored on externally exported functions
15
+ --> $DIR/inline-exported.rs:15:1
16
+ |
17
+ LL | #[inline]
18
+ | ^^^^^^^^^
19
+ |
20
+ = help: externally exported functions are functions with `#[no_mangle]`, `#[export_name]`, or `#[linkage]`
21
+
22
+ error: `#[inline]` is ignored on externally exported functions
23
+ --> $DIR/inline-exported.rs:20:1
24
+ |
25
+ LL | #[inline]
26
+ | ^^^^^^^^^
27
+ |
28
+ = help: externally exported functions are functions with `#[no_mangle]`, `#[export_name]`, or `#[linkage]`
29
+
30
+ error: aborting due to 3 previous errors
31
+
Original file line number Diff line number Diff line change 98
98
LL | trait Baz {}
99
99
| ------------ not a function definition
100
100
101
+ error: cannot use `#[inline(always)]` with `#[target_feature]`
102
+ --> $DIR/invalid-attribute.rs:69:1
103
+ |
104
+ LL | #[inline(always)]
105
+ | ^^^^^^^^^^^^^^^^^
106
+
101
107
error: attribute should be applied to a function definition
102
108
--> $DIR/invalid-attribute.rs:74:1
103
109
|
@@ -163,12 +169,6 @@ error: malformed `target_feature` attribute input
163
169
LL | #[target_feature(disable = "baz")]
164
170
| ^^^^^^^^^^^^^^^ help: must be of the form: `enable = ".."`
165
171
166
- error: cannot use `#[inline(always)]` with `#[target_feature]`
167
- --> $DIR/invalid-attribute.rs:69:1
168
- |
169
- LL | #[inline(always)]
170
- | ^^^^^^^^^^^^^^^^^
171
-
172
172
error[E0046]: not all trait items implemented, missing: `foo`
173
173
--> $DIR/invalid-attribute.rs:81:1
174
174
|
You can’t perform that action at this time.
0 commit comments