Jump to page: 1 2
Thread overview
[Issue 19569] overload resolution not right?
Jan 10
Manu
Jan 10
Manu
Jan 10
Manu
Jan 10
Manu
Jan 11
Manu
Jan 11
Manu
Jan 12
Manu
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

Jacob Carlborg <doob@me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob@me.com

--- Comment #1 from Jacob Carlborg <doob@me.com> ---
Attributes for templates are inferred. "void foo(T)();" will be inferred to be
"nothrow" if the arguments are "nothrow". Attributes are not inferred for
regular functions.

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #2 from Manu <turkeyman@gmail.com> ---
I'm not sure what you just said.

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #3 from Manu <turkeyman@gmail.com> ---
I mean, if there's not a really good reason those 2 pieces of code don't work the same, then they should work the same...

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

Mike Franklin <slavo5150@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |slavo5150@yahoo.com

--- Comment #4 from Mike Franklin <slavo5150@yahoo.com> ---
I think what Jacob is saying is that `nothrow` will be inferred for templates at the time of instantiation based on the context of the instatiation.

That means you should not create both `void foo(T)()` and `void foo(T)()
nothrow`.  When the template is instantiated the compiler will automatically
add `nothrow` to the instantiation if the context is `nothrow`.

The bug here may be that the compiler allowed you to declare both a `nothrow` and non-`nothrow` template without error.  I suppose it should be an error to add any inferred attribute to templates if they're going to inferred by the caller.  Either that, or, if the user explicitly added an attribute to a template, then inference for that attribute should be disabled at the time of instantiation.

I can't say I approve of this attribute inference behavior -- I'd prefer to have some mataprogramming facilities to control it -- but that's the way D currently seems to work, so changing it would probably require a DIP.

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com

--- Comment #5 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
Still wrong. Consider:

void foo()() { throw new Exception(""); } // Definitely not nothrow
void foo()() nothrow { } // definitely is nothrow

void test()
{
    foo();
}
void test() nothrow
{
    foo();
}

This gives the exact same error as before, demonstrating that this is indeed a problem exactly the way Manu describes it. However, if both variants of foo are indeed inferred to be nothrow, the criticisms presented by Jacob are correct.

As for 'if the user explicitly added an attribute to a template, then inference for that attribute should be disabled at the time of instantiation.', consider this:

void bar()() nothrow { throw new Exception(""); }
unittest { bar(); }

That gives the error message 'nothrow function bar!().bar may throw', so that already is disabled - if a templated function is marked nothrow, it has to abide by that promise.

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #6 from Manu <turkeyman@gmail.com> ---
> so changing it would probably require a DIP.

Look at my example; any reasonable observer would say those cases should behave
the same.
I think it's objectively a bug, that shouldn't need a DIP...

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #7 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
> any reasonable observer would say those cases should behave the same.

Here I have to disagree - both templates will be nothrow, so the compiler is correct to be confused. Look at my example above for a case where it's actually not ambiguous and still shows the same error message.

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #8 from Manu <turkeyman@gmail.com> ---
> both templates will be nothrow, so the compiler is correct to be confused.

How can they both be nothrow? One is nothrow, the other is not... they're extern, so they can't infer anything. The instantiated templates should have identical signatures to the functions above.

> Look at my example above for a case where it's actually not ambiguous and still shows the same error message.

Right... but that's a bug, right? The 2 functions definitely have different (and correct) signatures, so the overload resolution should work just like the simple case?

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #9 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
> Right... but that's a bug, right? The 2 functions definitely have different (and correct) signatures, so the overload resolution should work just like the simple case?

Absotively. I back you 100% in that this is a bug, and should absolutely not require a DIP in any form.


> How can they both be nothrow? One is nothrow, the other is not... they're extern, so they can't infer anything. The instantiated templates should have identical signatures to the functions above.

Ah, you're right - I forgot extern templates are a thing. For completion then, an example of how attributes are indeed not inferred for extern templates:

void foo()();

void test() nothrow
{
    foo(); // Error: function `foo!().foo` is not `nothrow`
}

--
January 10
https://issues.dlang.org/show_bug.cgi?id=19569

--- Comment #10 from Jacob Carlborg <doob@me.com> ---
(In reply to Manu from comment #8)

> How can they both be nothrow? One is nothrow, the other is not... they're extern, so they can't infer anything.

Oh, they don't have a body, I didn't think of that. Then I don't see how it could infer anything.

--
« First   ‹ Prev
1 2