tree-sitter-rust icon indicating copy to clipboard operation
tree-sitter-rust copied to clipboard

Array and tuple expressions accept inner attributes, not regular attributes

Open marijnh opened this issue 5 years ago • 3 comments
trafficstars

In array_expression and tuple_expression, I'm pretty sure the attributes after the opening bracket should be inner_attribute_item, not attribute_item. See https://doc.rust-lang.org/reference/expressions/array-expr.html . There's a test in declarations.txt that expects the current behavior, and should also be adjusted.

marijnh avatar Sep 29 '20 07:09 marijnh

It looks like both types of attributes may be allowed. And in fact, it seems that attribute_item can appear before any element of the array.

First, I tested that the behavior shown in the current test is valid:

fn main() {
   let a = [#[cfg(test)] 1, 2, 3];
   let t = (#[cfg(test)] 4, 5, 6);
   eprintln!("{:?} {:?}", a, t);
}

The code compiles fine with rustc 1.45-nightly. It appears that the attribute_item here applies to the first element of the array/tuple (removing the corresponding element, because I'm not compiling in test mode):

rustc test.rs
./test
[2, 3] (5, 6)

It turns out, a sequence of attribute_items can occur before each element of the array:

fn main() {
   let a = [#[cfg(test)] 1, 2, #[cfg(test)] 3];
   let t = (#[cfg(test)] 4, 5, #[cfg(test)] 6);
   eprintln!("{:?} {:?}", a, t);
}

Output:

[2] (5,)

Inner attributes seem to be allowed as well. I tried with this code:

fn main() {
   let a = [[#![cfg(test)] 1, 2, 3], [1, 2, 3]];
   let t = ((#![cfg(test)] 4, 5, 6), (4, 5, 6));
   eprintln!("{:?} {:?}", a, t);
}

As expected, the inner attribute caused the entire (nested) array/tuple to be omitted from its parent:

[[1, 2, 3]] ((4, 5, 6),)

maxbrunsfeld avatar Sep 29 '20 16:09 maxbrunsfeld

Ah, right, the regular attribute support in front of expressions (any expression, I guess) appears to be an experimental feature. So yeah, not sure in how far you support experimental stuff (tracking an evolving language with a grammar is its own can of worms).

marijnh avatar Sep 29 '20 19:09 marijnh

Seems like the inner attributes inside arrays or tuples are no longer valid, the code does not compile w/ rustc 1.78.0 nightly with this error:

error: an inner attribute is not permitted in this context

The regular attributes should be added

amaanq avatar Feb 17 '24 21:02 amaanq