Bug 22327 – [Templates] arguments of T[] literals work for exactly one initialization before being set in stone

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-22T00:00:59Z
Last change time
2024-12-13T19:18:37Z
Assigned to
crazymonkyyy
Creator
crazymonkyyy
Moved to GitHub: dmd#19988 →

Attachments

IDFilenameSummaryContent-TypeSize
1827iminhell.dminimizedtext/x-dsrc114
1828leethax.d:Dtext/x-dsrc225

Comments

Comment #0 by crazymonkyyy — 2021-09-22T00:00:59Z
Created attachment 1827 minimized given a template header such as `struct foo(T,T[] data)`, T[] data appears to mean whatever the first init defined T as This is across scope, functions and unit tests, if the first initization is int,[1,2,3], and you try string.["a","b","c"], it attempts to parse ["a","b","c"] as an int[]
Comment #1 by crazymonkyyy — 2021-09-22T00:32:19Z
Created attachment 1828 :D disregard its a feature not a bug ``` struct foo(T,T[] data){ enum data_=data; } unittest{ struct sercret{ enum message="i like cake"; } foo!(sercret,[]) bar; } unittest{ alias leet=typeof(foo!(int,[]).data_[0]); import std.stdio; leet.message.writeln; } ``` ``` 1 monkyyy@no ~/src/statemachines (git)-[master] % dmd -unittest -main -run leethax.d i like cake ```
Comment #2 by schveiguy — 2021-09-22T01:44:31Z
This is still a legitimate bug.
Comment #3 by crazymonkyyy — 2021-09-22T01:46:37Z
Comment #4 by snarwin+bugzilla — 2021-11-04T22:57:51Z
Example program (simplified from attached "minimized" example): --- template foo(T, T[] somedata_) {} alias bar = foo!(int, [1, 2, 3]); alias faz = foo!(string, ["a", "b", "c"]); --- Expected behavior: compiles successfully. Actual behavior: fails to compile, with the following error message: --- Error: template instance `foo!(string, ["a", "b", "c"])` does not match template declaration `foo(T, int[] somedata_)` --- Removing the line `alias bar = foo!(int, [1, 2, 3]);` causes compilation to succeed.
Comment #5 by maxhaton — 2021-11-05T02:02:34Z
------------------ TemplateInstance.findBestMatch() foo(T, T[] somedata_) @ 0x7fe5e3c38810 hash is 781bbdffb091 ++foo(T, T[] somedata_) It's a match with template declaration 'foo(T, int[] somedata_)' ++foo(T, int[] somedata_) at end of scope ------------------ Inside findBestMatch it looks like the template is considered and (for the first time round) matches successfully, but the process of doing this actually changes the TemplateDeclaration itself as shown above (see the difference between the two lines marked with pluses).
Comment #6 by nick — 2024-08-22T16:38:45Z
Reading comment 4, this looks like a duplicate of issue 22540.
Comment #7 by robert.schadek — 2024-12-13T19:18:37Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19988 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB