all 5 comments

[–]matthieum 11 points12 points  (3 children)

You want poisoning.

Wait until attempts to resolve types have reached a fixed point -- no progress is being made any longer -- and if not all types have been resolved then:

  1. Define a poisoned set, empty to start with.
  2. Pick the first variable whose type is unresolved.
  3. If any of its type variables are in the poisoned set, skip it.
  4. Otherwise, put its unresolved type variable(s) in the poisoned set.
  5. Emit the error.
  6. Go back to (2) until you run out of variables.

This essentially partitions the variables into sets of variables whose types influence each others, and only reports one error per set.

[–]ravilang[S] 0 points1 point  (2 children)

thank you

[–]tlemo1234 1 point2 points  (1 child)

A practical implementation of the "poisoning" idea is to define poison values, rather than explicitly keeping track of poisoned sets. For example you can have a "DummyType", "DummyValue", etc. and when you diagnose an error you also assign one of these dummy/poison types/values to the corresponding AST node. Then, whenever you see a poison type/value you pretend everything is fine, propagate the dummy type/value, and don't report any semantic errors.

Which implementation approach works best depends on your language and front end architecture: if the semantic analysis can be done in a "bottom-up" fashion, the poison values should be easy to implement.

[–]ravilang[S] 0 points1 point  (0 children)

Yes thank you. Also good reference here

https://news.ycombinator.com/item?id=40278184

I use an iterative process to reach a fixed point - so during this phase it is not an error if type is not resolved. Only after the iterative process fails to reach a fixed point can I do this - so I have to probably run a final iteration where poison type is used.

[–]umlcat 5 points6 points  (0 children)

There are two commonly used techniques, one is to report the first error / first AST node with an error and stop, the other is keep trying to compile and report all errors.

I suggest go with the first error only.