[Issue 5115] std.typecons.scoped problems

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 10 23:12:26 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5115



--- Comment #9 from Kenji Hara <k.hara.pg at gmail.com> 2011-11-10 23:11:38 PST ---
(In reply to comment #8)
> scoped!Foo() returns a temporary of type scoped!(Foo).Scoped (or something like
> that).
> This temporary is implicitly converted to Foo using alias this, but the
> temporary never has it's destructor called due to bug 3516, which means Foo's
> destructor is never called either.
> 
> It is fine for the temporary to be converted to Foo, so long as the destructor
> is called when the scope that 'scoped' was called in is exited.

Now bug 3516 was fixed. Then comment#1 code prints two "Foo.dtor"

But allowing conversion from temporary type (e.g. Scoped!Foo) to Foo is unsafe,
because the temporary has value type and its lifetime is limited in its scope,
but Foo is class reference of the temporary and we can bring it out the scope.

Foo global_foo;
void test1() {
    auto foo = scoped!Foo();
    global_foo = foo;   // implicitly conversion from typeof(foo) to Foo
      // This line should be forbidden in compile time
}
void test2() {
    // use global_foo -> Access Violation!
}
void main() {
    test1();
    test2();
}

I think my ProxyOf mixin template is useful for this issue.
https://github.com/D-Programming-Language/phobos/pull/300

--- Comment #10 from Kenji Hara <k.hara.pg at gmail.com> 2011-11-10 23:11:39 PST ---
(In reply to comment #8)
> scoped!Foo() returns a temporary of type scoped!(Foo).Scoped (or something like
> that).
> This temporary is implicitly converted to Foo using alias this, but the
> temporary never has it's destructor called due to bug 3516, which means Foo's
> destructor is never called either.
> 
> It is fine for the temporary to be converted to Foo, so long as the destructor
> is called when the scope that 'scoped' was called in is exited.

Now bug 3516 was fixed. Then comment#1 code prints two "Foo.dtor"

But allowing conversion from temporary type (e.g. Scoped!Foo) to Foo is unsafe,
because the temporary has value type and its lifetime is limited in its scope,
but Foo is class reference of the temporary and we can bring it out the scope.

Foo global_foo;
void test1() {
    auto foo = scoped!Foo();
    global_foo = foo;   // implicitly conversion from typeof(foo) to Foo
      // This line should be forbidden in compile time
}
void test2() {
    // use global_foo -> Access Violation!
}
void main() {
    test1();
    test2();
}

I think my ProxyOf mixin template is useful for this issue.
https://github.com/D-Programming-Language/phobos/pull/300

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list