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

Invalid char literal error message has an unhelpful snippet #30033

Closed
apasel422 opened this issue Nov 24, 2015 · 4 comments
Closed

Invalid char literal error message has an unhelpful snippet #30033

apasel422 opened this issue Nov 24, 2015 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST.

Comments

@apasel422
Copy link
Contributor

foo.rs:

fn main() {
    let _ = 'ab';
}
foo.rs:2:16: 2:18 error: character literal may only contain one codepoint: ';
foo.rs:2     let _ = 'ab';
                        ^~

The snippet at the end of the error message is not helpful, and should probably encompass everything between the two 's.

@apasel422 apasel422 added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. labels Nov 24, 2015
@gchp
Copy link
Contributor

gchp commented Jan 6, 2016

I get a different error for your example.

test.rs:2:16: 2:18 error: unterminated character constant: ';
test.rs:2     let _ = 'ab';
                         ^~
$ rustc -V
rustc 1.5.0 (3d7cd77e4 2015-12-04)

@apasel422
Copy link
Contributor Author

The error message from the original issue remains the same on nightly.

@gchp
Copy link
Contributor

gchp commented Jan 6, 2016

Ah, sorry, I didn't test with nightly. I see the error now.

@gchp
Copy link
Contributor

gchp commented Jan 6, 2016

Gonna take a crack at this issue. Should have a PR up soon, seems simple enough.

gchp added a commit to gchp/rust that referenced this issue Jan 14, 2016
Given this code:

    fn main() {
        let _ = 'abcd';
    }

The compiler would give a message like:

    error: character literal may only contain one codepoint: ';
    let _ = 'abcd';
                 ^~

With this change, the message now displays:

    error: character literal may only contain one codepoint: 'abcd'
    let _ = 'abcd'
            ^~~~~~

Fixes rust-lang#30033
bors added a commit that referenced this issue Jan 15, 2016
This is achieved by adding the scan_back method. This method looks back
through the source_text of the StringReader until it finds the target
char, returning it's offset in the source. We use this method to find
the offset of the opening single quote, and use that offset as the start
of the error.

Given this code:

```rust
fn main() {
    let _ = 'abcd';
}
```

The compiler would give a message like:

```
error: character literal may only contain one codepoint: ';
let _ = 'abcd';
             ^~
```
With this change, the message now displays:

```
error: character literal may only contain one codepoint: 'abcd';
let _ = 'abcd';
        ^~~~~~~
```

Fixes #30033
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST.
Projects
None yet
Development

No branches or pull requests

2 participants