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

honor --declaredLocs in more places, including type mismatch errors; also show kind with --declaredLocs #15673

Merged
merged 5 commits into from
Oct 27, 2020

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Oct 21, 2020

followup after #15666

  • honor --declaredLocs in more places, including type mismatch errors, see example 1
  • show (humanized) symbol kind in declared in message, which helps with cryptic errors, see example 2

example 1

block:
  type Foo = ref object
  type Foo2 = object
  type Bar[T]=object
  var b: Bar[float]
  b = Foo()

before PR:

t11164.nim(77, 10) Error: type mismatch: got <Foo> but expected 'Bar[system.float]'
    b = Foo()

after PR, when passing --declaredLocs --listfullpaths:off:

t11164.nim(77, 10) Error: type mismatch: got <Foo> [ref declared in t11164.nim(73, 8)] but expected 'Bar[system.float]' [object declared in t11164.nim(75, 8)]
    b = Foo()

example 2

block:
  type Foo = object
  proc fun(a: ref) = discard
  fun(Foo())

before PR:

t11164.nim(43, 6) Error: type mismatch: got <Foo>
but expected one of:
proc fun(a: ref)
  first type mismatch at position: 1
  required type for a: ref
  but expression 'Foo()' is of type: Foo

=> cryptic, the error doesn't tell you why Foo doesn't match unless you know that Foo is of kind object instead of ref
This is a common problem in my experience.

after PR, when passing --declaredLocs --listfullpaths:off:

t11164.nim(43, 6) Error: type mismatch: got <Foo>
but expected one of:
proc fun(a: ref) [proc declared in t11164.nim(42, 8)]
  first type mismatch at position: 1
  required type for a: ref [builtInTypeClass declared in t11164.nim(42, 12)]
  but expression 'Foo()' is of type: Foo [object declared in t11164.nim(39, 8)]

=> now you have all the context to understand the mismatch: fun expects a ref, but Foo is of kind object

future work

  • we should also honor --declaredLocs for cannot instantiate errors:
block:
  type Foo[T:ref] = ref object
  type Bar = object
  type Foo1 = Foo[Bar]

currently still gives

t11164.nim(29, 18) Error: cannot instantiate Foo
got: <type Bar>
but expected: <T: ref>
    type Foo1 = Foo[Bar]

which has same drawback of lacking sufficient context to understand the error (especially in complicated cases)

=> #17745

@timotheecour timotheecour force-pushed the pr_declaredlocs_typemismatch branch from ccc062e to 3226d05 Compare October 21, 2020 23:39
@timotheecour timotheecour requested a review from Araq October 22, 2020 01:03
@alaviss
Copy link
Collaborator

alaviss commented Oct 22, 2020

I think it's a bit too long here. We can certainly borrow a page from C/C++ compilers and print the declaration as a separated hint, which would be much easier to read.

@timotheecour
Copy link
Member Author

timotheecour commented Oct 22, 2020

how about I change:

t11164.nim(77, 10) Error: type mismatch: got <Foo> [ref declared in t11164.nim(73, 8)] but expected 'Bar[system.float]' [object declared in t11164.nim(75, 8)]

to

t11164.nim(77, 10) Error: type mismatch: got <Foo> [ref declared in t11164.nim(73, 8)]
  but expected 'Bar[system.float]' [object declared in t11164.nim(75, 8)]

with --declaredLocs:on; and keep the old formatting without newline with --declaredLocs:off

this will be consistent with sigmatch error formatting (got on 1 line, candidates on subsequent lines)

@Araq
Copy link
Member

Araq commented Oct 22, 2020

If you spend more time on it, do it properly. The problem is an error message like

"expected AbsoluteFile but got AbsoluteFile (= distinct string)"

which is caused by different modules that both export AbsoluteFile which is caused by --path issues. So add some logic to make the error message better for "expected X but got X", there is no need for yet another compiler switch that nobody knows about...

@timotheecour timotheecour force-pushed the pr_declaredlocs_typemismatch branch from 3226d05 to 527c532 Compare October 23, 2020 10:36
@timotheecour
Copy link
Member Author

timotheecour commented Oct 23, 2020

@Araq PTAL

I think it's a bit too long here. We can certainly borrow a page from C/C++ compilers and print the declaration as a separated hint, which would be much easier to read.

improved formatting, should now read well whether or not declaration locations are shown

add some logic to make the error message better for "expected X but got X"

done, the declaration locations are now shown when either the names clash or when --declaredlocs is provided

example

  • with name clash:
t11164.nim(114, 10) Error: type mismatch:
 got <Foo> [object declared in t11164.nim(112, 8)]
 but expected 'Foo = object' [object declared in t11164b.nim(2, 8)]
  • without name clash:
t11164.nim(120, 10) Error: type mismatch: got <Foo> but expected 'Foo2 = object'
t11164.nim(120, 10) Error: type mismatch:
 got <Foo> [object declared in t11164.nim(118, 8)]
 but expected 'Foo2 = object' [object declared in t11164b.nim(6, 8)]

(all above with --listfullpaths:off)

@Araq Araq merged commit 0fb8783 into nim-lang:devel Oct 27, 2020
@timotheecour timotheecour deleted the pr_declaredlocs_typemismatch branch October 27, 2020 17:23
mildred pushed a commit to mildred/Nim that referenced this pull request Jan 11, 2021
…also show `kind` with --declaredLocs (nim-lang#15673)

* honor --declaredLocs in more places, including type mismatch errors
* fix tests
* show declaration location also when type mismatch names clash
irdassis pushed a commit to irdassis/Nim that referenced this pull request Mar 16, 2021
…also show `kind` with --declaredLocs (nim-lang#15673)

* honor --declaredLocs in more places, including type mismatch errors
* fix tests
* show declaration location also when type mismatch names clash
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
…also show `kind` with --declaredLocs (nim-lang#15673)

* honor --declaredLocs in more places, including type mismatch errors
* fix tests
* show declaration location also when type mismatch names clash
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.

3 participants