Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax: ignoring tuple index with a suffix (a.1lolololol) #59418

Closed
kpp opened this issue Mar 25, 2019 · 4 comments
Closed

Syntax: ignoring tuple index with a suffix (a.1lolololol) #59418

kpp opened this issue Mar 25, 2019 · 4 comments
Assignees
Labels
A-parser Area: The parsing of Rust source code to an AST. C-bug Category: This is a bug. regression-from-stable-to-stable Performance or correctness regression from one stable version to another.

Comments

@kpp
Copy link
Contributor

kpp commented Mar 25, 2019

fn main() {
    let a = (1, 2, 3);
    println!("{}", a.1lolololol);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4280ded6698f9f9f3877ad0498769cb7

Expected:

ERROR: tuple index with a suffix is invalid

Got:

Output: 2\n

All thanks to: https://linuxrocks.online/@carl/101569506337640753

UPD:

The same error arises with tuple struct:

struct X(i32,i32,i32);

fn main() {
    let a = X(1, 2, 3);
    let b = a.1lolololol;
    println!("{}", b);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ad7f39e8edad0d98f2919bd4fdfded30

Expected:

ERROR: tuple index with a suffix is invalid

Got:

Output: 2\n
@kpp kpp changed the title Syntax: ignoring text after tuple index Syntax: ignoring text after tuple index (a.1lolololol) Mar 25, 2019
@kpp kpp changed the title Syntax: ignoring text after tuple index (a.1lolololol) Syntax: ignoring text after tuple/tuple struct index (a.1lolololol) Mar 25, 2019
@estebank
Copy link
Contributor

Introduced in 1.27. Output in 1.26:

error: tuple index with a suffix is invalid
 --> <source>:3:22
  |
3 |     println!("{}", a.1lolololol);
  |                      ^^^^^^^^^^

@kpp kpp changed the title Syntax: ignoring text after tuple/tuple struct index (a.1lolololol) Syntax: ignoring tuple index with a suffix (a.1lolololol) Mar 25, 2019
@estebank estebank self-assigned this Mar 25, 2019
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. C-bug Category: This is a bug. labels Mar 25, 2019
@varkor
Copy link
Member

varkor commented Mar 25, 2019

I have a fix for this.

Edit: ah, @estebank's already self-assigned — I'll leave it to them. One thing I would do, as well as introducing a custom error message, is make sure the suffix isn't ignored in the main code path. E.g.

token::Literal(token::Integer(name), _) => {
let span = self.span;
self.bump();
let field = ExprKind::Field(e, Ident::new(name, span));
e = self.mk_expr(lo.to(span), field, ThinVec::new());
}

would be better changed to something like:

token::Literal(token::Integer(name), suf) => {
    let span = self.span;
    self.bump();
    // Affix the suffix to the integer. This means that the suffix will
    // be treated as part of the field name and will not simply be ignored.
    let name = name.as_str().to_string() + suf.map_or("", |s| s.as_str().get());
    let field = ExprKind::Field(e, Ident::new(Symbol::intern(&name), span));
    e = self.mk_expr(lo.to(span), field, ThinVec::new());
}

so that even without the custom error message, the suffix is still taken into account when resolving the field name.

@estebank
Copy link
Contributor

@varkor I have the fix, just waiting for --bless to finish:

                    token::Literal(token::Integer(name), suffix) => {
                        let span = self.span;
                        self.bump();
                        let field = ExprKind::Field(e, Ident::new(name, span));
                        e = self.mk_expr(lo.to(span), field, ThinVec::new());
+                       if let Some(suffix) = suffix {
+                           let mut err = self.diagnostic().struct_span_err(
+                               span,
+                               "tuple index with a suffix is invalid",
+                           );
+                           err.span_label(span, format!("invalid suffix `{}`", suffix));
+                           err.emit();
+                       }
                    }

Centril added a commit to Centril/rust that referenced this issue Mar 27, 2019
…ochenkov

Reject integer suffix when tuple indexing

Fix rust-lang#59418.

r? @varkor
Centril added a commit to Centril/rust that referenced this issue Mar 27, 2019
…ochenkov

Reject integer suffix when tuple indexing

Fix rust-lang#59418.

r? @varkor
cuviper added a commit to cuviper/rust that referenced this issue Mar 28, 2019
…ochenkov

Reject integer suffix when tuple indexing

Fix rust-lang#59418.

r? @varkor
@Centril
Copy link
Contributor

Centril commented Apr 20, 2019

cc #59553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST. C-bug Category: This is a bug. regression-from-stable-to-stable Performance or correctness regression from one stable version to another.
Projects
None yet
Development

No branches or pull requests

4 participants