Bug 15781 – [REG2.069] Template type deduction failure with same-type variables with different constness
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-03-09T10:36:00Z
Last change time
2016-03-19T19:27:18Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
jbc.engelen
Comments
Comment #0 by jbc.engelen — 2016-03-09T10:36:47Z
```
struct TypeA {
int value;
}
auto foo(T)(T start, T end) {
import std.stdio;
writeln(typeid(T));
}
void main() {
alias Tiep = TypeA;
const Tiep a;
Tiep b;
Tiep c = a; // This is fine
foo(c,b);
foo(a,b); // This is not fine with DMD 2.069.2
}
```
The code compiles fine with DMD 2.068.2.
The code fails to compile with DMD 2.068.2 and DMD 2.070.2:
deduce.d(19): Error: template deduce.foo cannot deduce function from argument types !()(const(TypeA), TypeA), candidates are:
deduce.d(5): deduce.foo(T)(T start, T end)
I believe this is a regression.
DMD 2.068.2 deduces T = TypeA (non-const).
For `alias Tiep = int;` it compiles with DMD 2.070.2.
Also when modifying the code to read `auto foo(T)(const T start, const T end)`.
This issue was reported after a brief forum discussion: http://forum.dlang.org/thread/[email protected].
(In reply to Johan Engelen from comment #0)
> The code compiles fine with DMD 2.068.2.
> The code fails to compile with DMD 2.068.2 and DMD 2.070.2:
> deduce.d(19): Error: template deduce.foo cannot deduce function from
> argument types !()(const(TypeA), TypeA), candidates are:
> deduce.d(5): deduce.foo(T)(T start, T end)
>
> I believe this is a regression.
> DMD 2.068.2 deduces T = TypeA (non-const).
This is a rejects-valid issue from 2.069, so is a regression. But, the behavior in 2.068 -- T is deduced to TypeA (non-const) -- is not intentional result, because there was order-dependent bug.
struct S
{
int value;
}
void foo(T)(T a, T b)
{
pragma(msg, T);
}
void main()
{
const S cs;
S ms;
foo(cs, ms); // prints 'S'
foo(ms, cs); // prints 'const(S)', inconsistent!
}
Therefore, from the built-in common type calculation mechanism, the T should be deduced to const(S) independent from the order of function arguments.