Skip to content

Commit dc56fc1

Browse files
authored
Merge pull request #4662 from Cauterite/patch-1
make Unique.opDot() inout
2 parents 9f7dcd5 + e50b77f commit dc56fc1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

std/typecons.d

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public:
164164
return u;
165165
}
166166
/** Forwards member access to contents. */
167-
RefT opDot() { return _p; }
167+
auto opDot() inout { return _p; }
168168

169169
/**
170170
Postblit operator is undefined to prevent the cloning of $(D Unique) objects.
@@ -288,6 +288,26 @@ private:
288288
assert(!uf2.isEmpty);
289289
}
290290

291+
// ensure Unique behaves correctly through const access paths
292+
@system unittest
293+
{
294+
struct Bar {int val;}
295+
struct Foo
296+
{
297+
Unique!Bar bar = new Bar;
298+
}
299+
300+
Foo foo;
301+
foo.bar.val = 6;
302+
const Foo* ptr = &foo;
303+
static assert(is(typeof(ptr) == const(Foo*)));
304+
static assert(is(typeof(ptr.bar) == const(Unique!Bar)));
305+
static assert(is(typeof(ptr.bar.val) == const(int)));
306+
assert(ptr.bar.val == 6);
307+
foo.bar.val = 7;
308+
assert(ptr.bar.val == 7);
309+
}
310+
291311
// Used in Tuple.toString
292312
private template sharedToString(alias field)
293313
if (is(typeof(field) == shared))

0 commit comments

Comments
 (0)