Skip to content

Fix #1430: Better error messages for type errors involving type variables #1434

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

Merged
merged 11 commits into from
Aug 17, 2016

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Aug 1, 2016

Two fixes: (1) Report bounds instead of the variables themselves. (2) Avoid interpolation if there are unreported errors. Also, clean up of error message mentioned in #1424.

Review by @smarter.

@smarter
Copy link
Member

smarter commented Aug 1, 2016

The found part could also be improved:

class Inv[T](x: T)
object Test {
  val x: Inv[String] = new Inv(new Inv(1))
}
error: type mismatch:
 found   : Inv[T]
 required: String
  val x: Inv[String] = new Inv(new Inv(1))
                                   ^

@odersky
Copy link
Contributor Author

odersky commented Aug 2, 2016

@smarter The Inv example is a bit harder because T is invariant. So we cannot approximate with a bound like we did in the previous commit. I'll experiment with an auxiliary clause.

@smarter
Copy link
Member

smarter commented Aug 2, 2016

T is invariant but it's also lower-bounded and upper-bounded by Inv[Int] so I don't think an auxiliary clause is necessary there

@odersky
Copy link
Contributor Author

odersky commented Aug 7, 2016

This has evolved into a rather big refactoring affecting many of the error messages in the compiler. We can now systematically explain ambiguous names and type variables as appendices to error messages. errmsgs.check gives a taste.

}
}

/** The d string interpolator works like the i string interpolator, but marks nonsensical errors
Copy link
Member

Choose a reason for hiding this comment

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

Now called em and not d

@smarter
Copy link
Member

smarter commented Aug 7, 2016

This is neat, but the disambiguation system should have a special case for aliases, consider:

scala> List[String](List("foo")) 
<console>:5: error: type mismatch:
 found   : scala.collection.immutable.List[String]
 required: String'

where  String   is a class in package lang
       String'  is a type in object Predef$ which is an alias of String

Contrast with scalac:

scala> List[String](List("foo"))
<console>:12: error: type mismatch;
 found   : List[String]
 required: String

@smarter
Copy link
Member

smarter commented Aug 7, 2016

(We also need to do something about printing "object Predef$" instead of "object Predef" but that's a separate issue)

@smarter
Copy link
Member

smarter commented Aug 8, 2016

We should also look into eliding prefixes (like scala.collection.immutable.List -> List) to avoid this sort of things:

scala> object A { type T = Int; val x: T = "foo" } 
<console>:4: error: type mismatch:
 found   : String("foo")
 required: line18$object.$iw.$iw'.A.T

where  $iw   is a object in object line18$object$
       $iw'  is a object

@smarter
Copy link
Member

smarter commented Aug 8, 2016

Probably over-thinking this but instead of T, T', T'', ..., we could use unicode subscripts: T, T₁, T₂, ... which makes it clearer that this is not part of the name of the type. No clue if that works well in the Windows console though.

@smarter
Copy link
Member

smarter commented Aug 8, 2016

We could also optionally use different colors for disambiguation :)

@odersky
Copy link
Contributor Author

odersky commented Aug 13, 2016

@smarter I am open to other ways to indicate variants (color or subscripts are both is nice!) but we should know it works on all terminals.

odersky added 10 commits August 16, 2016 17:32
When issuing a type mismatch error, avoid mentioning polyparams
in the current constraint set and their associated typevars. Mention
instead the bound that caused the constrained to become unsatisfiable
(if that bound is unique, i.e. the parameter appears co- or contravariantly
in the type).
Interpolating typevars that appear co- or contra-variantly in a type
is a cleanup measure - it helps keep the constraint set small. However,
if there are uneported errors, some of these errors might report on
unsatisfiable constraints for these type variables. In that case, instantiating
the type variables risks being confusing.
Remove debug info from error message.
 Roll `sm` and `i` into one interpolator (also called `i`)
 Evolve `d` to `em` interpolator (for error messages)
 New interpolator `ex` with more explanations, replaces disambiguation.
Needs to read several input lines at once.

Enables repl test of new error messages.
Normal show will propagate the excpetions. Previously, exceptions were filtered
in both cases, which was redundant. Also, it's good to have a way to show things that
does not mask exceptions, if only to debug problems in show itself.
Don't disambiguate in situations like

    Predef.String

vs

    java.lang.String

where one Symbol is an alias of another with the same name.

Also, fix reviewer comments wrt comments and unused defs.
This was already disabled when printing types. Now is also disabled
when printing fully qualified names.
... when printing using RefinedPrinter. PlainPrinter will still show them.
The previous fix was too drastic, as it would also have omitted
scala, Prefef and other "unqualified owner types" from full names.
We now omit only "empty prefixes", i.e. roots, anonymous classes and
repl qualifiers.
@odersky odersky force-pushed the fix-#1430 branch 2 times, most recently from 546c92c to 3a2efae Compare August 16, 2016 15:40
@odersky
Copy link
Contributor Author

odersky commented Aug 16, 2016

Rebased to master to avoid merge conflicts.

Any objections to merge this now? Otherwise this PR will be in perennial conflict with everything else that's likely to be committed.

@odersky
Copy link
Contributor Author

odersky commented Aug 16, 2016

/rebuild

@smarter
Copy link
Member

smarter commented Aug 16, 2016

LGTM assuming the tests pass

@felixmulder
Copy link
Contributor

It looks like the CI has stalled on all PRs?

@odersky
Copy link
Contributor Author

odersky commented Aug 17, 2016

/rebuild

@smarter
Copy link
Member

smarter commented Aug 17, 2016

I amended the last commit to force the CI to run again, seems OK now.

@smarter smarter merged commit 83adc75 into scala:master Aug 17, 2016
@allanrenucci allanrenucci deleted the fix-#1430 branch December 14, 2017 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants