ahrefs / atd

Static types for JSON APIs
Other
315 stars 52 forks source link

atdd: use `alias this` for SumTypes #394

Closed elrandar closed 10 months ago

elrandar commented 10 months ago
Khady commented 10 months ago

@elrandar was the discussion which lead to that fix public? If yes then it would be cool to add a link to it. Otherwise we'll merge as is.

elrandar commented 10 months ago

Its in the public dlang discord server, but not really accessible for anyone that is not already a member. Here is a link to it.

Copy pasted contents of the relevant messages (accessed 23/11/23):


Ok, I found the problem.

  1. In SumType's destructor, it uses std.traits.hasElaborateDestructorto check whether it needs to run the destructor of the stored value.
  2. Internally, to determine whether a struct has a destructor, hasElaborateDestructor iterates over its fields to see if any of them have a destructor.
  3. In some cases, depending on the order in which the compiler processes the declarations, this will happen before it has finished processing the fields of Bar. When that happens, it results in the following error:
    (spec:1) /usr/include/dmd/druntime/import/core/internal/traits.d(16): Error: unable to determine fields of `Bar` because of forward references

Anyway, the good news is that there is a workaround. You can make the code compile by wrapping your SumType in a struct with an alias this declaration: [...] if you wrap the SumType up in a struct, the name lookup finds struct s and stops there, without looking inside.