[Issue 19126] Compiler removes inout on templated parameter and then complains it's not there

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 5 17:27:45 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19126

--- Comment #12 from Ali Ak <ali.akhtarzada at gmail.com> ---
(In reply to Steven Schveighoffer from comment #11)
> 
> I'm still finding trouble understanding why you want to specify immutable
> explicitly as the T value. Why not just mutable T? Then you don't need that
> machinery at all.

It's not that I want to or don't want to, it's that I think it should work
consistently.

> 
> > When T is an immutable it seems like the compiler should be doing this by
> > itself. If it decides to "replace" inout(immutable) with just immutable,
> > then it should replace inout everywhere with immutable. SO why doesn't
> > inout(W!(immutable int)) become immutable(T!int)? That would also make this
> > error go away right?
> 
> But that's not what you specified. inout(SomeTemplate!(immutable(T))) cannot
> be replaced in all cases with immmutable(SomeTemplate!T). In this case, it
> would be OK, but the compiler would have to do a lot to prove that.

Ah true.

> 
> > Maybe the compiler shouldn't be reducing inout in the first place? It is
> > supposed to be (IIUC) qualifiers in => qualifiers out right? So why does it
> > remove it before the it "lands" somewhere? This makes no sense regardless of
> > immutable being the strongest qualifier. The purposes of inout and immutable
> > seem orthogonal. It's not a mutability qualifier, it's a "transport of
> > mutability information" qualifier right?
> 
> Its behavior is the least common denominator of all 3 mutability parameters.
> The things you can do with inout are all the things you could do with a
> mutable, immutable, or const version, and nothing more.

Yes, so it feels weird that since it acts as a common denominator, the compiler
throws it away before it's done acting O_o

> 
> However, any qualifier that is added on top of immutable is immutable, so
> the compiler is stripping away inout to simplify things.

Ya I understand that's what's happening now. It just sounds like the compiler
is doing the wrong thing by removing inout in this case. Because it leads to a
compiler error with code that should "obviously" work.

It would be nice to just find this simplification line (if it even is a line)
in the compiler and see if just making it not "simplify" removal of inout (i
guess any inner inouts can be removed) will just keep everything working
including this case. I'm just not familiar enough with dmd to be able to give
this a try just yet.


> 
> But it can only do this on types that are fully immutable. In the case of
> the struct, only the member is immutable, it's not immutable on the whole
> struct.
> 
> Again, the best fix for this is simply not to instantiate with immutable(T).
> You can achieve everything without it.

Well I guess that's more a "it works if you do it like this" solution than a
fix :) D supports explicit template instantiation as a feature of the language
no? But it's not "working" in this case - T can be given any qualified type,
inout acts as a common denominator => seems like this should work.

--


More information about the Digitalmars-d-bugs mailing list