digitalmars.D - Deduction regression or improvement?
- Johan Engelen (17/17) Mar 08 2016 Hi all,
- Stefan Koch (3/20) Mar 08 2016 const T is not T
- H. S. Teoh via Digitalmars-d (8/26) Mar 08 2016 [...]
- Johan Engelen (9/35) Mar 08 2016 What was surprising to me is that 2.068.2 deduces T = SomeStruct.
- H. S. Teoh via Digitalmars-d (9/30) Mar 08 2016 [...]
- Meta (3/6) Mar 09 2016 Does template type inference do implicit conversion? I thought it
- H. S. Teoh via Digitalmars-d (35/42) Mar 09 2016 [...]
- deadalnix (4/31) Mar 11 2016 More tricky actually.
- Timon Gehr (5/16) Mar 12 2016 Actually the documentation currently special-cases pointers and arrays:
- Dicebot (6/30) Mar 08 2016 Looks like regression to me.
- Johan Engelen (2/2) Mar 09 2016 I've reported it here:
- Hara Kenji (24/30) Mar 11 2016 But the behavior with 2.068 was bug-prone.
- Johan Engelen (3/6) Mar 11 2016 I also thought it should deduce T == const(S), and was surprised
- Dicebot (4/11) Mar 11 2016 Sorry, but to me this "fix" looks like harmful regression that
Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); } See http://dpaste.dzfl.pl/15581af64747 DMD 2.068.2 compiles and does not complain. DMD 2.069.2 gives deduction error: "cannot deduce function from argument types !()(const(SomeStruct), SomeStruct), candidates are: foo(T)(T start, T end)" Is this a regression or intended? I did not find something about it in the release notes. Thanks, Johan
Mar 08 2016
On Tuesday, 8 March 2016 at 21:22:35 UTC, Johan Engelen wrote:Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); } See http://dpaste.dzfl.pl/15581af64747 DMD 2.068.2 compiles and does not complain. DMD 2.069.2 gives deduction error: "cannot deduce function from argument types !()(const(SomeStruct), SomeStruct), candidates are: foo(T)(T start, T end)" Is this a regression or intended? I did not find something about it in the release notes. Thanks, Johanconst T is not T Therefore this code shall not compile.
Mar 08 2016
On Tue, Mar 08, 2016 at 09:22:35PM +0000, Johan Engelen via Digitalmars-d wrote:Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); } See http://dpaste.dzfl.pl/15581af64747 DMD 2.068.2 compiles and does not complain. DMD 2.069.2 gives deduction error: "cannot deduce function from argument types !()(const(SomeStruct), SomeStruct), candidates are: foo(T)(T start, T end)" Is this a regression or intended? I did not find something about it in the release notes.[...] IMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression. T -- If the comments and the code disagree, it's likely that *both* are wrong. -- Christopher
Mar 08 2016
On Tuesday, 8 March 2016 at 22:35:57 UTC, H. S. Teoh wrote:On Tue, Mar 08, 2016 at 09:22:35PM +0000, Johan Engelen via Digitalmars-d wrote:What was surprising to me is that 2.068.2 deduces T = SomeStruct. But SomeStruct c = a; works fine, i.e. copying const(SomeStruct) to a SomeStruct is fine. Argument passing a struct is just copying, so should be fine to deduce T=SomeStruct? ( it may help to play a little with the linked code at dpaste.dzfl.pl )Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); } See http://dpaste.dzfl.pl/15581af64747 DMD 2.068.2 compiles and does not complain. DMD 2.069.2 gives deduction error: "cannot deduce function from argument types !()(const(SomeStruct), SomeStruct), candidates are: foo(T)(T start, T end)" Is this a regression or intended? I did not find something about it in the release notes.[...] IMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression.
Mar 08 2016
On Tue, Mar 08, 2016 at 11:31:54PM +0000, Johan Engelen via Digitalmars-d wrote:On Tuesday, 8 March 2016 at 22:35:57 UTC, H. S. Teoh wrote:[...]On Tue, Mar 08, 2016 at 09:22:35PM +0000, Johan Engelen via Digitalmars-d wrote:Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); }[...] Hmm. I guess it makes sense if SomeStruct has no indirections, since structs are value types so you can always copy a const value to a mutable variable / parameter. T -- It is of the new things that men tire --- of fashions and proposals and improvements and change. It is the old things that startle and intoxicate. It is the old things that are young. -- G.K. ChestertonIMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression.What was surprising to me is that 2.068.2 deduces T = SomeStruct. But SomeStruct c = a; works fine, i.e. copying const(SomeStruct) to a SomeStruct is fine. Argument passing a struct is just copying, so should be fine to deduce T=SomeStruct?
Mar 08 2016
On Tuesday, 8 March 2016 at 22:35:57 UTC, H. S. Teoh wrote:IMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression.Does template type inference do implicit conversion? I thought it did not, and thus would expect this example to not compile.
Mar 09 2016
On Wed, Mar 09, 2016 at 02:42:51PM +0000, Meta via Digitalmars-d wrote:On Tuesday, 8 March 2016 at 22:35:57 UTC, H. S. Teoh wrote:[...] Hmm. I tried a little test, which revealed something interesting: class A {} class B : A {} class C : A {} void func(T)(T a, T b) {} void main() { auto x = new A; auto y = new B; auto z = new C; func(x,y); // OK func(x,z); // OK func(y,z); // compile error func(y,x); // OK func(z,x); // OK func(z,y); // compile error } It seems like *some* kind of implicit conversion is happening, because the compiler is able to figure out, in the case where one of the arguments is the base class, that it should convert the other argument to the base class. However, it doesn't seem to be able to figure out the case where neither argument is the base class, even though both does share the same base class. Arguably, this could be construed to be a bug / enhancement request? It does seem to imply that IFTI does include some form of implicit conversion check, though. T -- If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van SteenbergenIMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression.Does template type inference do implicit conversion? I thought it did not, and thus would expect this example to not compile.
Mar 09 2016
On Tuesday, 8 March 2016 at 22:35:57 UTC, H. S. Teoh wrote:On Tue, Mar 08, 2016 at 09:22:35PM +0000, Johan Engelen via Digitalmars-d wrote:More tricky actually. If T has indirection, then that is the correct answer. If not, the T == SomeStruct.Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); } See http://dpaste.dzfl.pl/15581af64747 DMD 2.068.2 compiles and does not complain. DMD 2.069.2 gives deduction error: "cannot deduce function from argument types !()(const(SomeStruct), SomeStruct), candidates are: foo(T)(T start, T end)" Is this a regression or intended? I did not find something about it in the release notes.[...] IMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression. T
Mar 11 2016
On 12.03.2016 08:58, deadalnix wrote:Actually the documentation currently special-cases pointers and arrays: "The deduced type parameter for dynamic array and pointer arguments has an unqualified head:" http://dlang.org/spec/template.html#function-templatesIMO, this *should* compile and infer T == const(SomeStruct) as the common type of a and b. But I'm not sure whether or not this is a regression. TMore tricky actually. If T has indirection, then that is the correct answer. If not, the T == SomeStruct.
Mar 12 2016
On 03/08/2016 11:22 PM, Johan Engelen wrote:Hi all, Should the following compile or not? auto foo(T)(T start, T end) {} void main() { const SomeStruct a; SomeStruct b; foo(a,b); } See http://dpaste.dzfl.pl/15581af64747 DMD 2.068.2 compiles and does not complain. DMD 2.069.2 gives deduction error: "cannot deduce function from argument types !()(const(SomeStruct), SomeStruct), candidates are: foo(T)(T start, T end)" Is this a regression or intended? I did not find something about it in the release notes. Thanks, JohanLooks like regression to me. If SomeStruct contains indirections, it should be able to deduce T as const(SomeStruct). It SomeStruct is strict value type, deducing T as both SomeStruct and const(SomeStruct) would be fine.
Mar 08 2016
I've reported it here: https://issues.dlang.org/show_bug.cgi?id=15781
Mar 09 2016
On Wednesday, 9 March 2016 at 01:27:41 UTC, Dicebot wrote:Looks like regression to me. If SomeStruct contains indirections, it should be able to deduce T as const(SomeStruct). It SomeStruct is strict value type, deducing T as both SomeStruct and const(SomeStruct) would be fine.But the behavior with 2.068 was bug-prone. struct S { int value; } auto foo(T)(T start, T end) { pragma(msg, "In foo, T = ", T); } void main() { const S cs; S ms; foo(cs, ms); // [a] NG with 2.069.2, but T == S with 2.068! foo(ms, cs); // [b] NG with 2.069.2, but T == const(S) with 2.068!! } The both cases are disabled in the bugfix PR: https://github.com/D-Programming-Language/dmd/pull/4818 I think that T should be deduced to the typeof(true ? cs : ms) at line [a] and typeof(true ? ms : cs) at line [b], then the two lines will get consistent deduction result T == const(S). Kenji Hara
Mar 11 2016
On Friday, 11 March 2016 at 13:28:22 UTC, Hara Kenji wrote:I think that T should be deduced to the typeof(true ? cs : ms) at line [a] and typeof(true ? ms : cs) at line [b], then the two lines will get consistent deduction result T == const(S).I also thought it should deduce T == const(S), and was surprised 2.068.2 deduced it without const.
Mar 11 2016
On Friday, 11 March 2016 at 13:28:22 UTC, Hara Kenji wrote:On Wednesday, 9 March 2016 at 01:27:41 UTC, Dicebot wrote: The both cases are disabled in the bugfix PR: https://github.com/D-Programming-Language/dmd/pull/4818 I think that T should be deduced to the typeof(true ? cs : ms) at line [a] and typeof(true ? ms : cs) at line [b], then the two lines will get consistent deduction result T == const(S). Kenji HaraSorry, but to me this "fix" looks like harmful regression that needs to be reverted. Bug prone or not it breaks valid good code without deprecation process.
Mar 11 2016