"potentially malformed `opDispatch`"

Nicholas Wilson iamthewilsonator at hotmail.com
Tue Sep 28 01:34:45 UTC 2021


On Monday, 27 September 2021 at 19:33:19 UTC, Ali Çehreli wrote:
> We talked about template instantiation problems hiding valuable 
> information before. Here is one:
>
> struct S {
>   auto opDispatch(string symbol)() {
>     static assert(symbol == "foo", "Invalid symbol.");
>   }
> }
>
> void main() {
>   S().bar;
> }
>
> Error: no property `bar` for type `deneme.S`
>        potentially malformed `opDispatch`. Use an explicit 
> instantiation to get a better error message
>
> opDispatch fails to compile because of my 'static assert' but 
> my message "Invalid symbol." is lost. Granted, the compiler 
> recommends me to do the following:
>
>   S().opDispatch!"bar";  // Ok, now I get my error message
>
> 1) Can't we display my message in the first place?

I couldn't figure out how to recreate the failed expression to 
issue the underlying error. See [this 
pr](https://github.com/dlang/dmd/pull/12288)

> 2) If we can't, can the compiler itself instantiate explicitly 
> to show the error message?

It might be possible, [this bit of 
code](https://github.com/dlang/dmd/blob/58653722e2be7abe61517505c06edf7680c933d1/src/dmd/typesem.d#L3602-L3629) looks like it does something similar.

> 3) If not even that, can we really expect a newcomer to figure 
> out what to do? At least we should apologize and provide the 
> expression for the programmer to try again with. :)

That should be possible by formatting [this 
string](https://github.com/dlang/dmd/blob/d396a9e89c9e2a7ae687616231ba392c9ba7859d/src/dmd/typesem.d#L2429) with:
     "...(rest of 
message)...`%s.opDispatch!\"%s\"`",mt.toPrettyChars(), 
ident.toChars()

PRs welcome!


More information about the Digitalmars-d mailing list