]> git.lizzy.rs Git - rust.git/commitdiff
Allow #[inline] on closures
authorAmanieu d'Antras <amanieu@gmail.com>
Fri, 27 Apr 2018 05:20:46 +0000 (07:20 +0200)
committerAmanieu d'Antras <amanieu@gmail.com>
Fri, 27 Apr 2018 10:34:01 +0000 (12:34 +0200)
Fixes #49632

src/librustc/hir/check_attr.rs
src/test/compile-fail/attr-usage-inline.rs
src/test/compile-fail/issue-31769.rs
src/test/compile-fail/issue-43988.rs
src/test/run-pass/issue-49632.rs [new file with mode: 0644]
src/test/ui/error-codes/E0518.stderr
src/test/ui/feature-gate/issue-43106-gating-of-inline.rs
src/test/ui/feature-gate/issue-43106-gating-of-inline.stderr

index 956cd17f38f214cd91f47550641f74cdb667742a..19f8d15662d84f7a0c93862e7d7becb2e3b76eb2 100644 (file)
@@ -30,6 +30,7 @@ enum Target {
     ForeignMod,
     Expression,
     Statement,
+    Closure,
     Other,
 }
 
@@ -103,14 +104,14 @@ fn check_attributes(&self, item: &hir::Item, target: Target) {
         self.check_repr(item, target);
     }
 
-    /// Check if an `#[inline]` is applied to a function.
+    /// Check if an `#[inline]` is applied to a function or a closure.
     fn check_inline(&self, attr: &hir::Attribute, span: &Span, target: Target) {
-        if target != Target::Fn {
+        if target != Target::Fn && target != Target::Closure {
             struct_span_err!(self.tcx.sess,
                              attr.span,
                              E0518,
-                             "attribute should be applied to function")
-                .span_label(*span, "not a function")
+                             "attribute should be applied to function or closure")
+                .span_label(*span, "not a function or closure")
                 .emit();
         }
     }
@@ -286,9 +287,13 @@ fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
     }
 
     fn check_expr_attributes(&self, expr: &hir::Expr) {
+        let target = match expr.node {
+            hir::ExprClosure(..) => Target::Closure,
+            _ => Target::Expression,
+        };
         for attr in expr.attrs.iter() {
             if attr.check_name("inline") {
-                self.check_inline(attr, &expr.span, Target::Expression);
+                self.check_inline(attr, &expr.span, target);
             }
             if attr.check_name("repr") {
                 self.emit_repr_error(
index c6b9b016331aa61d0ed1b257b34161f247a0b504..250905dbdcd8ca91b9aaacfcb81588f60f43f27c 100644 (file)
@@ -13,7 +13,7 @@
 #[inline]
 fn f() {}
 
-#[inline] //~ ERROR: attribute should be applied to function
+#[inline] //~ ERROR: attribute should be applied to function or closure
 struct S;
 
 fn main() {}
index 7f73d9076ec99eec8ec4665a1c16b7dc54843de0..2bd45deeab4df7e2d091c5941dafa0f977026143 100644 (file)
@@ -9,6 +9,6 @@
 // except according to those terms.
 
 fn main() {
-    #[inline] struct Foo;  //~ ERROR attribute should be applied to function
+    #[inline] struct Foo;  //~ ERROR attribute should be applied to function or closure
     #[repr(C)] fn foo() {} //~ ERROR attribute should be applied to struct, enum or union
 }
index ff1fdaef416c82ddb33fcfc2744d0a26fcf03758..0dfa9f6f0d3417f7c196d6590ed705ee9992a1ef 100644 (file)
@@ -14,12 +14,12 @@ fn main() {
 
     #[inline]
     let _a = 4;
-    //~^^ ERROR attribute should be applied to function
+    //~^^ ERROR attribute should be applied to function or closure
 
 
     #[inline(XYZ)]
     let _b = 4;
-    //~^^ ERROR attribute should be applied to function
+    //~^^ ERROR attribute should be applied to function or closure
 
     #[repr(nothing)]
     let _x = 0;
@@ -40,7 +40,7 @@ fn foo() {}
 
     #[inline(ABC)]
     foo();
-    //~^^ ERROR attribute should be applied to function
+    //~^^ ERROR attribute should be applied to function or closure
 
     let _z = #[repr] 1;
     //~^ ERROR attribute should not be applied to an expression
diff --git a/src/test/run-pass/issue-49632.rs b/src/test/run-pass/issue-49632.rs
new file mode 100644 (file)
index 0000000..8cbb7d2
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(stmt_expr_attributes)]
+
+pub fn main() {
+    let _x = #[inline(always)] || {};
+    let _y = #[inline(never)] || {};
+    let _z = #[inline] || {};
+}
index d8feec9914068c06e5da00b5cccb27902c3b22e8..27d5d3645fdfc358dd20c034940cb1853aa28ec3 100644 (file)
@@ -1,19 +1,19 @@
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/E0518.rs:11:1
    |
 LL | #[inline(always)] //~ ERROR: E0518
    | ^^^^^^^^^^^^^^^^^
 LL | struct Foo;
-   | ----------- not a function
+   | ----------- not a function or closure
 
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/E0518.rs:14:1
    |
 LL |   #[inline(never)] //~ ERROR: E0518
    |   ^^^^^^^^^^^^^^^^
 LL | / impl Foo {
 LL | | }
-   | |_- not a function
+   | |_- not a function or closure
 
 error: aborting due to 2 previous errors
 
index 410f960e655fbaeb466c4d6a95a1212273b72f98..b03faad988ebc1c79093695feced71f553b8643b 100644 (file)
 #![inline                     = "2100"]
 
 #[inline = "2100"]
-//~^ ERROR attribute should be applied to function
+//~^ ERROR attribute should be applied to function or closure
 mod inline {
     mod inner { #![inline="2100"] }
-    //~^ ERROR attribute should be applied to function
+    //~^ ERROR attribute should be applied to function or closure
 
     #[inline = "2100"] fn f() { }
 
     #[inline = "2100"] struct S;
-    //~^ ERROR attribute should be applied to function
+    //~^ ERROR attribute should be applied to function or closure
 
     #[inline = "2100"] type T = S;
-    //~^ ERROR attribute should be applied to function
+    //~^ ERROR attribute should be applied to function or closure
 
     #[inline = "2100"] impl S { }
-    //~^ ERROR attribute should be applied to function
+    //~^ ERROR attribute should be applied to function or closure
 }
 
 fn main() {}
index d67d78e31a9d5cf7d4587e802df2fed713b17a9b..4d63c3f50125d4348489c8fd583aea6111e7ce4f 100644 (file)
@@ -1,41 +1,41 @@
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/issue-43106-gating-of-inline.rs:21:1
    |
 LL |   #[inline = "2100"]
    |   ^^^^^^^^^^^^^^^^^^
-LL |   //~^ ERROR attribute should be applied to function
+LL |   //~^ ERROR attribute should be applied to function or closure
 LL | / mod inline {
 LL | |     mod inner { #![inline="2100"] }
-LL | |     //~^ ERROR attribute should be applied to function
+LL | |     //~^ ERROR attribute should be applied to function or closure
 LL | |
 ...  |
-LL | |     //~^ ERROR attribute should be applied to function
+LL | |     //~^ ERROR attribute should be applied to function or closure
 LL | | }
-   | |_- not a function
+   | |_- not a function or closure
 
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/issue-43106-gating-of-inline.rs:24:17
    |
 LL |     mod inner { #![inline="2100"] }
-   |     ------------^^^^^^^^^^^^^^^^^-- not a function
+   |     ------------^^^^^^^^^^^^^^^^^-- not a function or closure
 
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/issue-43106-gating-of-inline.rs:29:5
    |
 LL |     #[inline = "2100"] struct S;
-   |     ^^^^^^^^^^^^^^^^^^ --------- not a function
+   |     ^^^^^^^^^^^^^^^^^^ --------- not a function or closure
 
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/issue-43106-gating-of-inline.rs:32:5
    |
 LL |     #[inline = "2100"] type T = S;
-   |     ^^^^^^^^^^^^^^^^^^ ----------- not a function
+   |     ^^^^^^^^^^^^^^^^^^ ----------- not a function or closure
 
-error[E0518]: attribute should be applied to function
+error[E0518]: attribute should be applied to function or closure
   --> $DIR/issue-43106-gating-of-inline.rs:35:5
    |
 LL |     #[inline = "2100"] impl S { }
-   |     ^^^^^^^^^^^^^^^^^^ ---------- not a function
+   |     ^^^^^^^^^^^^^^^^^^ ---------- not a function or closure
 
 error: aborting due to 5 previous errors