Skip to content

Improve error message for char literals #28887

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Nov 5, 2015
Merged

Conversation

steveklabnik
Copy link
Member

If you try to put something that's bigger than a char into a char
literal, you get an error:

fn main() {
    let c = 'ஶ்ரீ';
}

error: unterminated character constant:

This is a very compiler-centric message. Yes, it's technically
'unterminated', but that's not what you, the user did wrong.

Instead, this commit changes it to

error: character literal that's larger than a char:

As this actually tells you what went wrong.

Fixes #28851

@rust-highfive
Copy link
Contributor

r? @sfackler

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member

This may want to also consider something like 'aa', for example:

foo.rs:2:8: 2:10 error: unterminated character constant: ';
foo.rs:2     'aa';
                ^~

Printing instead "character literal that's larger than a char" may also be a little misleading perhaps as a is "smaller than a char" technically. Perhaps something like "multiple code points found" or something like that?

@steveklabnik
Copy link
Member Author

Ahhh yes. I tried to think of a few different things, but you're right that this message has that weakness. I like "multiple code points found", if I change it to that, is this okay?

@nagisa
Copy link
Member

nagisa commented Oct 7, 2015

I’d change the error message to “char(acter) may only contain/represent 1 codepoint”.

Both the previous and suggested error are pretty nasty in case of combining characters like 'ō' or 'ぱ'. The current makes no sense in any case, and the new doesn’t really point out the reason… “'ō'/'ぱ' cannot fit into char even if they’re 1 codepoint? Oh well; I guess I’ll use a string.”

If we want to get fancy, we could even add a note pointing existence of combining codepoints in characters.

@steveklabnik
Copy link
Member Author

If we want to get fancy, we could even add a note pointing existence of combining codepoints in characters.

I think some notes would be really good, actually.

@steveklabnik
Copy link
Member Author

I've updated this with a better message. What do you think?

@bors
Copy link
Collaborator

bors commented Nov 3, 2015

☔ The latest upstream changes (presumably #29285) made this pull request unmergeable. Please resolve the merge conflicts.

@steveklabnik
Copy link
Member Author

Rebased.

@@ -1 +1 @@
Subproject commit cde1ed3196ba9b39bcf028e06e08a8722113a5cb
Subproject commit 62ad301a2407a7aca50c1d5120c63597d676d29f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidental update?

@alexcrichton
Copy link
Member

r=me

If you try to put something that's bigger than a char into a char
literal, you get an error:

    fn main() {
        let c = 'ஶ்ரீ';
    }

    error: unterminated character constant:

This is a very compiler-centric message. Yes, it's technically
'unterminated', but that's not what you, the user did wrong.

Instead, this commit changes it to

    error: character literal may only contain one codepoint

As this actually tells you what went wrong.

Fixes rust-lang#28851
@steveklabnik
Copy link
Member Author

@bors: r=alexcrichton

@bors
Copy link
Collaborator

bors commented Nov 5, 2015

📌 Commit 00e9ad1 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Nov 5, 2015

⌛ Testing commit 00e9ad1 with merge 792a9f1...

bors added a commit that referenced this pull request Nov 5, 2015
If you try to put something that's bigger than a char into a char
literal, you get an error:

    fn main() {
        let c = 'ஶ்ரீ';
    }

    error: unterminated character constant:

This is a very compiler-centric message. Yes, it's technically
'unterminated', but that's not what you, the user did wrong.

Instead, this commit changes it to

    error: character literal that's larger than a char:

As this actually tells you what went wrong.

Fixes #28851
@bors bors merged commit 00e9ad1 into rust-lang:master Nov 5, 2015
@steveklabnik steveklabnik deleted the gh28851 branch June 19, 2016 20:31
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants