August 14, 2013 Re: Typeinfo inconsistencies | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Tue, Aug 13, 2013 at 09:48:10PM +0200, Maxim Fomin wrote: > On Tuesday, 13 August 2013 at 17:42:31 UTC, H. S. Teoh wrote: > >So, I got some free time today and decided to look into issue 6210, and found out that the problem was caused by this: > > > > char[] a = "h".dup; > > const(char)[] b = "h"; > > string c = "h"; > > > > writeln(typeid(a).getHash(&a)); // prints 104 > > writeln(typeid(b).getHash(&b)); // prints 703014434222502 > > writeln(typeid(c).getHash(&c)); // prints 104 > > > >This is completely b0rken, because it causes the following code to fail: > > > > int[char[]] aa; > > aa["h"] = 1; > > > > assert(aa.dup == aa); // fails > > > >The reason for this inconsistency is that char[] and > >immutable(char)[] (aka string) have their getHash functions > >overridden in druntime's src/rt/typeinfo/ti_Ag.d, but there is no > >such override for const(char)[]. > > > >I tried adding another subclass for const(char)[]'s typeid that inherits the correct version of getHash, but it didn't work because presumably this stuff is hardcoded into the compiler somewhere. > > > >So my question is, where in the compiler decides to use the specific > >typeinfos for char[] and immutable(char)[], but not const(char)[]? > > > > > >T > > OK > > --- a/src/typinf.c > +++ b/src/typinf.c > @@ -790,7 +790,8 @@ int TypeDArray::builtinTypeInfo() > #if DMDV2 > return !mod && (next->isTypeBasic() != NULL && !next->mod || > // strings are so common, make them builtin > - next->ty == Tchar && next->mod == MODimmutable); > + next->ty == Tchar && next->mod == MODimmutable || > + next->ty == Tchar && next->mod == MODconst); > #else > return next->isTypeBasic() != NULL; > #endif > > --- a/src/rt/typeinfo/ti_Ag.d > +++ b/src/rt/typeinfo/ti_Ag.d > @@ -186,3 +186,15 @@ class TypeInfo_Aya : TypeInfo_Aa > } > } > > +// const(char)[] > + > +class TypeInfo_Axa : TypeInfo_Aa > +{ > + override string toString() const { return "const(char)[]"; } > + > + override @property inout(TypeInfo) next() inout > + { > + return cast(inout)typeid(const(char)); > + } > +} > + Thanks!! Will you submit a pull request? You can reference bug 6210. I'm guessing that this will probably also fix one or two other AA-related bugs that have been around for a while. T -- It's amazing how careful choice of punctuation can leave you hanging: |
Copyright © 1999-2021 by the D Language Foundation