Skip to content

WIP: mention if type defaulting is forced by -XMonomorphismRestriction

Fix #21434. Doesn't work yet.

As far as I can tell, the plumbing required to get the information from decideMonoTyVars — it should be a set of Vars — to the place where the defaulting is done / the warning is created, at least in a direct manner, is ... rather excessive. I followed the call stacks myself and noted down what called what in order to find the join, so to speak, of disambigGroup and decideMonoTyVars. It seems it would involve changing like 10 type signatures to thread the VarSet up like 10 functions and then down to around the part where the Cts are generated and fed to applyDefaultingRules / disambigGroup / warnDefaulting, just for a slightly improved diagnostic.

A less disruptive approach seems to be using some kind of environment to store the state, and I noticed that TcRn is, conveniently, a reader monad, so I tried doing the following:

  1. Add tcl_mr_tvs :: TcRef VarSet to TcLclEnv to store the list of tyvars that are forced to be monomorphic due to the monomorphism restriction.
  2. Populate that field from decideMonoTyVars.
  3. When generating the -Wtype-defaults warning, check if the tyvar being defaulted is in that VarSet, and if so, adapt the error to mention that the defaulting is due to the monomorphism restriction.

Not sure if that should work, but it doesn't. The tau thing doesn't match up — I think that's some kind of depth/level? (The variable name also seems to be a_aJa instead of a_aJA ... not sure if that is just coincidence and those are completely unrelated unique names.)

#21434 ()
  mr_tvs:  {a_aJa[tau:1]}
  the_tv:  a_aJA[tau:0]
  due_to_mr:  False
  tidy_tv:  Just a0_aJA[tau:0]
  due_to_mr:  False

Hopefully there's some easy way of reconciling these two sources of data so the membership check works.

As for the actual design of how the error is produced, that's something I intend to improve once I get the feature to actually work, likely by taking the approach @rae suggested in #21434 with a new CtOrigin wrapping the constraint and so on.

Edited by Soham Chowdhury

Merge request reports