Nim
Nim copied to clipboard
remove allowMetaTypes for inferStaticParam + properly annotate unresolved range
fixes #19923
The code in #19923 already compiles since #24005, but when actually trying to pass an argument to it, sigmatch fails to infer the array type (but does infer the range type). There are 2 reasons:
- The expression
self.S * 8is atyFromExprwhich for some reason results in the range type not being markedtfUnresolved, and so array types just test for range span equality. This is becausemakeRangeWithStaticExpronly setstfUnresolvedif the given expression type isn't nil and has a nil node which is never true fortyFromExpr. This is fixed by just manually settingtfUnresolved. (also for when the nodehasUnresolvedArgsbut this isn't necessary here) - The compiler can't resolve
self.Sat overload resolution time sinceinferStaticParamenablesallowMetaTypes, which results intyFromExprs never getting instantiated. To fix this we turn offallowMetaTypes.
Turning off allowMetaTypes here has several consequences:
- The types undergo
instCopyTypewhich changes their ID, so to give their bindings properly we have to get the original static type ID, which is done here by doing.sym.typon the static type, since thesymis invariant between type copies. Thankfully onlyinferStaticParamgives a binding here. - Inability to instantiate directly gives an error instead of waiting for overloads to finish. This is both due to the nature of
semtypinstand the call tofailureToInferStaticParam. In practice this isn't encountered often (mostly because it wasn't possible before) but it can cause unrelated overloads to be inaccessible if an overload requires explicit generic arguments. If this becomes a problem we can refactorsemtypinstandfailureToInferStaticParamto a version that just doesn't globally error on these and passes the information to the mismatch errors. Edit: Done in #24098
Inability to instantiate directly gives an error instead of waiting for overloads to finish
shouldn't this just cause it to not be considered? sounds similar to C++ sfinae, which is a nice feature in general..
shouldn't this just cause it to not be considered?
Yep, I started implementing it in #24098, which this PR should probably follow. Good point about SFINAE, I was debating some refactors (not doing inc c.inGenericContext when matching tyFromExpr as mentioned in #24095, restricting the "calls with no matching overloads are untyped" to just matching unresolved types) that would have made the compiler error on stuff like:
template foo(T: type int): untyped = float
proc bar[T](x: T) = echo "a"
proc bar[T](x: foo(T)) = echo "b"
bar(12.3) # a
bar[int](12.3) # b
bar[float](12.3) # error
Should probably write a spec about this stuff after it's all figured out
Edit: Actually this still errors because explicit instantiations can't handle it, but those refactors also would have broken it
This pull request is stale because it has been open for 1 year with no activity. Contribute more commits on the pull request and rebase it on the latest devel, or it will be closed in 30 days. Thank you for your contributions.
This pull request has been marked as stale and closed due to inactivity after 395 days.