[Issue 12606] Mismatch of known array length during dynamic => static array assignment should emit better diagnostics

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Apr 21 06:59:52 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=12606

Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg at gmail.com

--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
Kenji:

The way I see it, we need some way of returning back information on why some
implicit conversion didn't work.

For example, in 'implicitConvTo' in the visitor functions 'result' is simply
initialized to MATCHnomatch if the dimensions of the two slices don't match.
But this isn't propagate back to the caller side.

We could vastly improve diagnostics for type mismatches, length mismatches,
template instance failures, if we had a way of return back information.

I think one way we could do it is to turn the MATCH enum into a struct, which
internally holds more state. For example:

struct MATCH
{
    /// equivalent to what MATCH was before: MATCHnomatch, MATCHconvert...
    MATCHLevel level;  

    /// call this to emit informative diagnostic
    const char*(*error_fp)();
}

In internal matching routines the code could do (sorry this is a bit mixed-C/D
code syntax):

-----
if (e->elements->dim != tsa->dim->toInteger())
{
    result = MATCH(MATCHnomatch, 
        { return format("Array length mismatch: %s %s", e->elements->dim, 
                        tsa->dim->toInteger()); }
}
-----

So the idea is the routine initializes a pointer, and then the call site can
decide whether to call it to extract a diagnostic, or whether to ignore it. For
example the 'resolveFuncCall' function typically walks through several template
instances before it decides on emitting diagnostics, so having a lazy way of
extracting a diagnostic is cruical.

On the call site (e.g. 'resolveFuncCall') the code could look like:

-----
if (matchresult.level == MATCHnomatch)
{
    error("Failed to match: %s", matchresult.error_fp());
}
-----

What do you think about this design? I'm not sure whether we can create
delegates in C++ like we can in D though, I'm not too familiar with what's
supported there.

--


More information about the Digitalmars-d-bugs mailing list