Looking for feedback on an improved error message for E0308

Hi folks, I'm working on improving the error message for E0308. In this post, I'm hoping I can get some feedback on the wording I'm working on. I'm a new contributor to Rust, and I'm nervous about changing the error message so many will see when editing their code.

It's the error message when two different types seem to be assigned to the same variable. (Which doesn't work in a static language as I understand it. I'm a bit of a perpetual beginner to programming...)

I thought the old message was a bit wordy and intimidating, but that it could be communicated in a clearer way without losing any accuracy.

I just did this PR that got merged: https://github.com/rust-lang/rust/pull/70242/files

But I'm still a bit unsatisfied with how it turned out.

(I don't want to change the code snippet in the error message, because I think it's good. I'm focused only on the wording of the message.)

Here's my latest draft:

This error occurs when the compiler was unable to infer the concrete type of a variable. It can occur for several cases; The most common case is a mismatch between two types: the type explicitly assigned to a variable by the [code's] author, and the type the compiler infers for an expression assigned to that variable.

I'm not sure about the phrase "the code's author" being maybe too verbose... and I'm not sure if I want to add this at the end of the last sentence:

assigned to that variable [later on].

Or:

for an expression [to be assigned] to that variable.

I also want to add a "hint" sentence on to the end, explaining that this error exists due to Rust being a statically-typed language. This error seemed like a pretty foreign concept to me when I first saw it, because I'm used to dynamic typing. I have taken a few approaches to wording this, and I'm not sure which I prefer.

No variable may have more than one type during the same program, as Rust is statically typed.

A single variable may not have more than one type during the same program, as Rust is statically typed.

A single variable may not have more than one type, as Rust is a statically typed language.

A variable may not take on a second type after it has been initialized with a first type. (Rust is a statically typed language.)

Can anyone give me their thoughts on this? Suggested improved wording? Can you confirm my wording choices are accurate? Is it okay I took out the concept of "initialization," from the main error text, or would most programmers get better information with that included?

Thanks for any feedback.

Also: I'm interested in starting up an effort to improve the existing error messages and docs, and sort of set an example of clear yet approachable language around the Rust language. If anyone has ideas on how to do this or wants to join in, feel free to get in touch.

2 Likes

@TomP Thanks for the suggestion. I take that as being a proposed wording for the "hint" lines at the end.

That's nice and concise, and I trust that it's accurate. But its conciseness is partly due to not explaining what the difference between static and dynamic typing is.

As a beginner, I might not know what that means... I also might now "know what I don't know" enough to be confident doing a web search about it. I figure anyone who actually runs rustc --explain E0308 is mystified enough that a little more explanation is worth it.

But at the same time, more-advanced programmers might feel it's unnecessary, or too much hand-holding? ("painfully obvious"?) So I don't want to totally ignore their opinions either...

Would love some varied opinions in here so I can gauge the consensus. But at the end of the day (figuratively -- I'll take the time to get this right as long as I'm receiving replies) I'll submit the best I have as a second PR.

I don't feel that it's the job of Rust error messages to teach programming concepts. People who don't understand the terminology can use a search engine to find what the terminology means.

I think E0308 is an error people will see a lot as they try to pick up the language, so the hint is justified in my personal opinion.

Edit to add: I realized, when trying to capture the essence of this error code so I could improve the wording, that it's basically just "follow the rules of static typing, please." If you don't understand static typing, the error message makes no sense. So that's why I think a short sentence or two of explaining static typing is worthwhile and appropriate. Meeting the programmer where they're at.

I feel like within the length of prose already used, since before I got involved, these concepts can be referred to.

On the other hand, I've heard a couple of times so far (on Reddit) that error messages shouldn't "teach" programming concepts.

I do feel there's a missing piece of explanation in the Rust ecosystem that teaches these basic concepts. C resources often do explain programming concepts, as it's assumed to be the coder's first language. IMO a user trying to learn Rust as a beginner is less lucky in that regard...

If consensus is against it, I'll have to leave it out. My first instinct is to briefly touch on the meaning of static typing, though.

If all error messages assumed that rationale, error messages about lifetimes should try to teach about lifetimes. Likewise for traits, etc. Why not just provide a subcluse number for the appropriate part of TRPL as guidance for people who are trying to program in Rust without first studying the documentation?

If you're suggesting linking to The Book™, that might be a reasonable compromise. (I couldn't tell if you were sincerely for, or sarcastically against that.)

The problem being, The Book doesn't really explain static typing clearly, either!

Keep in mind that Rust is a statically typed language, which means that it must know the types of all variables at compile time.

This is the only mention of static typing I am aware of in The Book. It is not very explicit that a single variable may not have more than one type. If that much isn't clear, the user has to do a web search, hopefully find a clear explanation, maybe just give up... I don't want to send the reader of my error message text on a wild goose chase. It feels like a few dozen bytes of text is a fair price to pay in order to hint that a variable can't have more than one type. Anything short of such a direct explanation feels like innuendo or charades.

As for my previous point, my thinking was that some errors are more likely to be seen by beginners to programming/Rust in general. And other errors that involve more advanced concepts might assume a more-advanced reader.

Some amount of human judgment and leeway toward error message content is okay, rather than hard and fast rules, as long as it doesn't get out of hand or become an ongoing problem.

I doubt a reviewer at GitHub would ask me to remove such a brief explanation of static typing. I am ideologically opposed to withholding that information from readers on the basis of "not teaching programming concepts" as if that were a virtue. (I feel the opposite, that teaching is a virtue.) IMO it is on-topic and very brief, and therefore shouldn't be offensive, even to advanced readers. We all were beginners at one point...

My suggestion was sincere, not facetious. There are many aspects of Rust that are difficult to learn, depending on one's prior programming experience. I mentioned lifetimes and traits because those differ from most programming language experience. It makes sense to enhance many error messages for nauplii (new crustaceans) by adding one or more references to subclauses of TRPL, which is the offical Rust documentation. (For more esoteric topics the Rustonomicon may be the appropriate reference.)

For the specific topic of E0308, if the book explanation is too minimal, as your research seems to indicate, then it is the book that needs a better explanation of the topic. That, coupled with a reference to an appropriate subclause of the book, should suffice for E0308. The same approach can be applied anywhere else that the existing documentation is deemed insufficient.

Thanks for the considered response. I think that's reasonable, and I can probably tweak The Book a bit.

My last remaining concern is that The Book states in the introduction that it expects you to have learned programming in general by learning another language.

This book assumes that you’ve written code in another programming language but doesn’t make any assumptions about which one. We’ve tried to make the material broadly accessible to those from a wide variety of programming backgrounds. We don’t spend a lot of time talking about what programming is or how to think about it. If you’re entirely new to programming, you would be better served by reading a book that specifically provides an introduction to programming.

Thus, to learn Rust, I must first learn C, Python, Ruby or JavaScript etc. etc. and then come back and learn Rust. (Assuming I want to use the best Rust resource, The Rust Programming Language.)

Maybe I'm on my own, but I'd like to think lots of other people want to learn Rust as a first language... Maybe I'll just have to write such a resource myself one of these days. But that's arguably off-topic. So I'll mull over any improvements I can make to the [error message] text (without an aside to fundamentally explain static programming, I suppose).

@tomp may I use your suggested text in my Pull Request?

If so, should I credit you, and in what way?

Of course you can use it, preferably without credit. Lots of us contribute to discussions on open forums. No credit is expected, or needed.