Thread overview
"potentially malformed `opDispatch`"
Sep 27, 2021
Ali Çehreli
Sep 28, 2021
Ali Çehreli
Sep 28, 2021
Nicholas Wilson
September 27, 2021
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?

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

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. :)

Ali
September 27, 2021

On 9/27/21 3:33 PM, Ali Çehreli wrote:

>

We talked about template instantiation problems hiding valuable information before. Here is one:

I recently asked the same question, got mostly crickets: https://forum.dlang.org/post/sgb2m5$2eai$1@digitalmars.com

-Steve

September 27, 2021
On 9/27/21 12:45 PM, Steven Schveighoffer wrote:
> On 9/27/21 3:33 PM, Ali Çehreli wrote:
>> We talked about template instantiation problems hiding valuable information before. Here is one:
> 
> I recently asked the same question, got mostly crickets: https://forum.dlang.org/post/sgb2m5$2eai$1@digitalmars.com
> 
> -Steve

Oh! Ok, then... See you again next month... :)

Ali

September 28, 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!