Simple and effective approaches to constraint error messages

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 25 15:14:28 PDT 2016


On Mon, Apr 25, 2016 at 05:20:08PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> On 4/25/16 1:52 PM, Andrei Alexandrescu wrote:
> >It's been long asked in our community that failing template
> >constraints issue better error messages. Consider:
> 
> I like the first option. However, I think it should be deeper than
> that.
> 
> Sometimes you have code that you are sure matches one of the
> constraints (e.g. isInputRange), but for some reason it doesn't. Sure,
> it's good to know that your struct that looks exactly like an input
> range isn't an input range, but to know why would be better.
> 
> I realize that more context for an error may be too verbose, but an
> option to have the compiler tell you exactly why it is stopping
> compilation is good when you can't figure out the obvious reason.

What about displaying the full context with -v? The compiler currently
already uses -v to show long error messages that are truncated by
default.


> So for instance, having it say "constraint failed:
> isInputRange!NotARange" is good, but if you pass some parameter it
> says something like: "constraint failed: isInputRange!NotARange,
> std.range.primitives:146 failed to compile: x.front
[...]

What about this: when a constraint fails, display the first (related
group of) error messages related to that constraint that the compiler
would have emitted if errors weren't gagged. So if isInputRange fails to
instantiate for some argument, the compiler would show the first error
message that resulted in template instantiation failure, e.g.:

	std/range.d(123): Error: no property 'front' for type 'int'

It's not completely nice, in that it exposes the implementation
somewhat, but it seems to be more useful when something goes wrong to
see concrete code that's failing than to get a message about
isInputRangeImpl!(X,Y,Z) failing to compile, and you have no idea what
that's supposed to mean because it's an internal Phobos implementation
detail.


T

-- 
Perhaps the most widespread illusion is that if we were in power we would behave very differently from those who now hold it---when, in truth, in order to get power we would have to become very much like them. -- Unknown


More information about the Digitalmars-d mailing list