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

Error E0308 isn't relevant to the original error message #36596

Closed
adtac opened this issue Sep 20, 2016 · 1 comment
Closed

Error E0308 isn't relevant to the original error message #36596

adtac opened this issue Sep 20, 2016 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@adtac
Copy link

adtac commented Sep 20, 2016

Here's my code (2.rs):

fn main() {
    let x = 5;
    let a = if x == 5 { 1 };
    println!("{}", a);
}

On running rustc 2.rs, I get

2.rs:3:13: 3:28 error: if may be missing an else clause [E0308]
2.rs:3     let a = if x == 5 { 1 };
                   ^~~~~~~~~~~~~~~
2.rs:3:13: 3:28 help: run `rustc --explain E0308` to see a detailed explanation
2.rs:3:13: 3:28 note: expected type `()`
2.rs:3:13: 3:28 note:    found type `_`
error: aborting due to previous error

And rustc --explain E0308 gives

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 of which is a
mismatch in the expected type that the compiler inferred for a variable's
initializing expression, and the actual type explicitly assigned to the
variable.

For example:

let x: i32 = "I am not a number!";

...

Am I missing something? Shouldn't the explanation talk about error: if may be missing an else clause?

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 20, 2016
@TimNN
Copy link
Contributor

TimNN commented Sep 21, 2016

The error explanation was added in #24576.

The code reporting E0308 contains the following note:

        // FIXME: do we want to use a different error code for each origin?
        let mut diag = struct_span_err!(
            self.tcx.sess, trace.origin.span(), E0308,
            "{}", trace.origin.as_failure_str()
        );

And, from impl TypeOrigin:

    fn as_failure_str(&self) -> &'static str {
        match self {
            &TypeOrigin::Misc(_) |
            &TypeOrigin::RelateOutputImplTypes(_) |
            &TypeOrigin::ExprAssignable(_) => "mismatched types",
            &TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait",
            &TypeOrigin::MatchExpressionArm(.., source) => match source {
                hir::MatchSource::IfLetDesugar{..} => "`if let` arms have incompatible types",
                _ => "match arms have incompatible types",
            },
            &TypeOrigin::IfExpression(_) => "if and else have incompatible types",
            &TypeOrigin::IfExpressionWithNoElse(_) => "if may be missing an else clause",
            &TypeOrigin::RangeExpression(_) => "start and end of range have incompatible types",
            &TypeOrigin::EquatePredicate(_) => "equality predicate not satisfied",
            &TypeOrigin::MainFunctionType(_) => "main function has wrong type",
            &TypeOrigin::StartFunctionType(_) => "start function has wrong type",
            &TypeOrigin::IntrinsicType(_) => "intrinsic has wrong type",
            &TypeOrigin::MethodReceiver(_) => "mismatched method receiver",
        }
    }

jfirebaugh added a commit to jfirebaugh/rust that referenced this issue Oct 2, 2016
Introduce the possibility of assigning distinct error codes to the various origin types of E0308. Start by assigning E0317 for the "IfExpressionWithNoElse" case, and write a long diagnostic specific to this case.

Fixes rust-lang#36596
bors added a commit that referenced this issue Oct 17, 2016
Use a distinct error code for "if may be missing an else clause"

Introduce the possibility of assigning distinct error codes to the various origin types of E0308. Start by assigning E0317 for the "IfExpressionWithNoElse" case, and write a long diagnostic specific to this case.

Fixes #36596
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
Projects
None yet
Development

No branches or pull requests

3 participants