Bug 19129 – std.typecons.scoped crashes when used on classes that have context

Status
NEW
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-07-30T18:22:13Z
Last change time
2024-12-01T16:34:03Z
Keywords
industry
Assigned to
No Owner
Creator
Eyal
Moved to GitHub: phobos#10346 →

Comments

Comment #0 by eyal — 2018-07-30T18:22:13Z
Example: int y; class C { this(int x) { y = x; } } import std.typecons: scoped; scoped!C(1); // crashes, as ctx.y segfaults when ctx remains null scoped should probably reject classes that have a "this" member (that aren't `static class`).
Comment #1 by simen.kjaras — 2018-07-30T19:36:58Z
This issue is also present in std.conv.emplace (which scoped uses internally). At least currently, it is impossible to get a good value for the context pointer: unittest { int y; class C { this(int x) { y = x; } } create!C(); } T create(T)() { // outer function context of foo.__unittest_L1_C1 is // needed to new nested class foo.__unittest_L1_C1.C return new T(0); } emplace already checks for isInnerClass!T, and should also check for nested, non-inner classes. std.traits should have a trait that checks for this (hasContext? isNestedClass sounds like it should include isInnerClass).
Comment #2 by simen.kjaras — 2018-09-20T09:22:59Z
template hasContextPointer(T) { struct S(Fields...) { Fields f; } enum fieldSize = S!(typeof(T.tupleof)).sizeof; static if (is(T == class)) { enum hasContextPointer = __traits(classInstanceSize, T) > fieldSize + size_t.sizeof*2 -1; } else { enum hasContextPointer = T.sizeof > fieldSize; } }
Comment #3 by robert.schadek — 2024-12-01T16:34:03Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10346 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB