Skip to content

Commit 5f2c111

Browse files
committed
Allow #[inline] on closures
Fixes #49632
1 parent 9822b57 commit 5f2c111

File tree

8 files changed

+54
-32
lines changed

8 files changed

+54
-32
lines changed

src/librustc/hir/check_attr.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum Target {
3030
ForeignMod,
3131
Expression,
3232
Statement,
33+
Closure,
3334
Other,
3435
}
3536

@@ -103,14 +104,14 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
103104
self.check_repr(item, target);
104105
}
105106

106-
/// Check if an `#[inline]` is applied to a function.
107+
/// Check if an `#[inline]` is applied to a function or a closure.
107108
fn check_inline(&self, attr: &hir::Attribute, span: &Span, target: Target) {
108-
if target != Target::Fn {
109+
if target != Target::Fn && target != Target::Closure {
109110
struct_span_err!(self.tcx.sess,
110111
attr.span,
111112
E0518,
112-
"attribute should be applied to function")
113-
.span_label(*span, "not a function")
113+
"attribute should be applied to function or closure")
114+
.span_label(*span, "not a function or closure")
114115
.emit();
115116
}
116117
}
@@ -286,9 +287,13 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
286287
}
287288

288289
fn check_expr_attributes(&self, expr: &hir::Expr) {
290+
let target = match expr.node {
291+
hir::ExprClosure(..) => Target::Closure,
292+
_ => Target::Expression,
293+
};
289294
for attr in expr.attrs.iter() {
290295
if attr.check_name("inline") {
291-
self.check_inline(attr, &expr.span, Target::Expression);
296+
self.check_inline(attr, &expr.span, target);
292297
}
293298
if attr.check_name("repr") {
294299
self.emit_repr_error(

src/test/compile-fail/attr-usage-inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[inline]
1414
fn f() {}
1515

16-
#[inline] //~ ERROR: attribute should be applied to function
16+
#[inline] //~ ERROR: attribute should be applied to function or closure
1717
struct S;
1818

1919
fn main() {}

src/test/compile-fail/issue-31769.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
#[inline] struct Foo; //~ ERROR attribute should be applied to function
12+
#[inline] struct Foo; //~ ERROR attribute should be applied to function or closure
1313
#[repr(C)] fn foo() {} //~ ERROR attribute should be applied to struct, enum or union
1414
}

src/test/compile-fail/issue-43988.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ fn main() {
1414

1515
#[inline]
1616
let _a = 4;
17-
//~^^ ERROR attribute should be applied to function
17+
//~^^ ERROR attribute should be applied to function or closure
1818

1919

2020
#[inline(XYZ)]
2121
let _b = 4;
22-
//~^^ ERROR attribute should be applied to function
22+
//~^^ ERROR attribute should be applied to function or closure
2323

2424
#[repr(nothing)]
2525
let _x = 0;
@@ -40,7 +40,7 @@ fn main() {
4040

4141
#[inline(ABC)]
4242
foo();
43-
//~^^ ERROR attribute should be applied to function
43+
//~^^ ERROR attribute should be applied to function or closure
4444

4545
let _z = #[repr] 1;
4646
//~^ ERROR attribute should not be applied to an expression

src/test/run-pass/issue-49632.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(stmt_expr_attributes)]
12+
13+
pub fn main() {
14+
let _x = #[inline(always)] || {};
15+
let _y = #[inline(never)] || {};
16+
let _z = #[inline] || {};
17+
}

src/test/ui/error-codes/E0518.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
error[E0518]: attribute should be applied to function
1+
error[E0518]: attribute should be applied to function or closure
22
--> $DIR/E0518.rs:11:1
33
|
44
LL | #[inline(always)] //~ ERROR: E0518
55
| ^^^^^^^^^^^^^^^^^
66
LL | struct Foo;
7-
| ----------- not a function
7+
| ----------- not a function or closure
88

9-
error[E0518]: attribute should be applied to function
9+
error[E0518]: attribute should be applied to function or closure
1010
--> $DIR/E0518.rs:14:1
1111
|
1212
LL | #[inline(never)] //~ ERROR: E0518
1313
| ^^^^^^^^^^^^^^^^
1414
LL | / impl Foo {
1515
LL | | }
16-
| |_- not a function
16+
| |_- not a function or closure
1717

1818
error: aborting due to 2 previous errors
1919

src/test/ui/feature-gate/issue-43106-gating-of-inline.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
#![inline = "2100"]
2020

2121
#[inline = "2100"]
22-
//~^ ERROR attribute should be applied to function
22+
//~^ ERROR attribute should be applied to function or closure
2323
mod inline {
2424
mod inner { #![inline="2100"] }
25-
//~^ ERROR attribute should be applied to function
25+
//~^ ERROR attribute should be applied to function or closure
2626

2727
#[inline = "2100"] fn f() { }
2828

2929
#[inline = "2100"] struct S;
30-
//~^ ERROR attribute should be applied to function
30+
//~^ ERROR attribute should be applied to function or closure
3131

3232
#[inline = "2100"] type T = S;
33-
//~^ ERROR attribute should be applied to function
33+
//~^ ERROR attribute should be applied to function or closure
3434

3535
#[inline = "2100"] impl S { }
36-
//~^ ERROR attribute should be applied to function
36+
//~^ ERROR attribute should be applied to function or closure
3737
}
3838

3939
fn main() {}

src/test/ui/feature-gate/issue-43106-gating-of-inline.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
error[E0518]: attribute should be applied to function
1+
error[E0518]: attribute should be applied to function or closure
22
--> $DIR/issue-43106-gating-of-inline.rs:21:1
33
|
44
LL | #[inline = "2100"]
55
| ^^^^^^^^^^^^^^^^^^
6-
LL | //~^ ERROR attribute should be applied to function
6+
LL | //~^ ERROR attribute should be applied to function or closure
77
LL | / mod inline {
88
LL | | mod inner { #![inline="2100"] }
9-
LL | | //~^ ERROR attribute should be applied to function
9+
LL | | //~^ ERROR attribute should be applied to function or closure
1010
LL | |
1111
... |
12-
LL | | //~^ ERROR attribute should be applied to function
12+
LL | | //~^ ERROR attribute should be applied to function or closure
1313
LL | | }
14-
| |_- not a function
14+
| |_- not a function or closure
1515

16-
error[E0518]: attribute should be applied to function
16+
error[E0518]: attribute should be applied to function or closure
1717
--> $DIR/issue-43106-gating-of-inline.rs:24:17
1818
|
1919
LL | mod inner { #![inline="2100"] }
20-
| ------------^^^^^^^^^^^^^^^^^-- not a function
20+
| ------------^^^^^^^^^^^^^^^^^-- not a function or closure
2121

22-
error[E0518]: attribute should be applied to function
22+
error[E0518]: attribute should be applied to function or closure
2323
--> $DIR/issue-43106-gating-of-inline.rs:29:5
2424
|
2525
LL | #[inline = "2100"] struct S;
26-
| ^^^^^^^^^^^^^^^^^^ --------- not a function
26+
| ^^^^^^^^^^^^^^^^^^ --------- not a function or closure
2727

28-
error[E0518]: attribute should be applied to function
28+
error[E0518]: attribute should be applied to function or closure
2929
--> $DIR/issue-43106-gating-of-inline.rs:32:5
3030
|
3131
LL | #[inline = "2100"] type T = S;
32-
| ^^^^^^^^^^^^^^^^^^ ----------- not a function
32+
| ^^^^^^^^^^^^^^^^^^ ----------- not a function or closure
3333

34-
error[E0518]: attribute should be applied to function
34+
error[E0518]: attribute should be applied to function or closure
3535
--> $DIR/issue-43106-gating-of-inline.rs:35:5
3636
|
3737
LL | #[inline = "2100"] impl S { }
38-
| ^^^^^^^^^^^^^^^^^^ ---------- not a function
38+
| ^^^^^^^^^^^^^^^^^^ ---------- not a function or closure
3939

4040
error: aborting due to 5 previous errors
4141

0 commit comments

Comments
 (0)