Skip to content

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

Closed
@kpp

Description

@kpp
Contributor
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

Activity

changed the title [-]Syntax: ignoring text after tuple index[/-] [+]Syntax: ignoring text after tuple index (a.1lolololol)[/+] on Mar 25, 2019
changed the title [-]Syntax: ignoring text after tuple index (a.1lolololol)[/-] [+]Syntax: ignoring text after tuple/tuple struct index (a.1lolololol)[/+] on Mar 25, 2019
estebank

estebank commented on Mar 25, 2019

@estebank
Contributor

Introduced in 1.27. Output in 1.26:

error: tuple index with a suffix is invalid
 --> <source>:3:22
  |
3 |     println!("{}", a.1lolololol);
  |                      ^^^^^^^^^^
changed the title [-]Syntax: ignoring text after tuple/tuple struct index (a.1lolololol)[/-] [+]Syntax: ignoring tuple index with a suffix (a.1lolololol)[/+] on Mar 25, 2019
self-assigned this
on Mar 25, 2019
added
A-parserArea: The lexing & parsing of Rust source code to an AST
regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
C-bugCategory: This is a bug.
on Mar 25, 2019
varkor

varkor commented on Mar 25, 2019

@varkor
Member

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

estebank commented on Mar 25, 2019

@estebank
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();
+                       }
                    }
added 2 commits that reference this issue on Mar 27, 2019

Rollup merge of rust-lang#59421 - estebank:tuple-index-suffix, r=petr…

892c18b

Rollup merge of rust-lang#59421 - estebank:tuple-index-suffix, r=petr…

d3119e4
added a commit that references this issue on Mar 28, 2019

Rollup merge of rust-lang#59421 - estebank:tuple-index-suffix, r=petr…

be34621
Centril

Centril commented on Apr 20, 2019

@Centril
Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @kpp@Centril@estebank@varkor

      Issue actions

        Syntax: ignoring tuple index with a suffix (a.1lolololol) · Issue #59418 · rust-lang/rust