Skip to content

Commit

Permalink
Fix issue #21640: @LiVe not working with templates
Browse files Browse the repository at this point in the history
Signed-off-by: Luís Ferreira <contact@lsferreira.net>
  • Loading branch information
ljmf00 authored and dlang-bot committed Feb 16, 2021
1 parent 4ec62c1 commit d4b989f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dmd/mtype.d
Expand Up @@ -4267,6 +4267,7 @@ extern (C++) final class TypeFunction : TypeNext
t.mod = mod;
t.isnothrow = isnothrow;
t.isnogc = isnogc;
t.islive = islive;
t.purity = purity;
t.isproperty = isproperty;
t.isref = isref;
Expand Down
44 changes: 44 additions & 0 deletions test/compilable/ob2.d
@@ -0,0 +1,44 @@
// REQUIRED_ARGS: -preview=dip1021

void* malloc();

@live T* foo1(T)(T* p)
{
return p; // consumes owner
}

@live T* foo2(T)()
{
T* p = null;
return p; // consumes owner
}

@live T* foo3(T)(T* p)
{
scope T* q = p; // borrows from p
return p; // use of p ends borrow in q
}

@live T* foo4(T)(T* p)
{
scope T* bq = p; // borrow
scope const T* cq = p; // const borrow
return p; // ends both borrows
}

@live T* foo5(T)()
{
auto p = cast(T*) malloc();
scope b = p;
return p;
}

void test()
{
int* p;
foo1!int(p);
foo2!int();
foo3!int(p);
foo4!int(p);
foo5!int();
}
67 changes: 67 additions & 0 deletions test/fail_compilation/failob2.d
@@ -0,0 +1,67 @@
// REQUIRED_ARGS: -preview=dip1021

/*
TEST_OUTPUT:
---
fail_compilation/failob2.d(105): Error: variable `failob2.foo1!int.foo1.p` has undefined state and cannot be read
fail_compilation/failob2.d(105): Error: variable `failob2.foo1!int.foo1.p` is returned but is Undefined
fail_compilation/failob2.d(124): Error: template instance `failob2.foo1!int` error instantiating
fail_compilation/failob2.d(111): Error: variable `failob2.foo2!int.foo2.p` has undefined state and cannot be read
fail_compilation/failob2.d(111): Error: variable `failob2.foo2!int.foo2.p` is returned but is Undefined
fail_compilation/failob2.d(125): Error: template instance `failob2.foo2!int` error instantiating
fail_compilation/failob2.d(119): Error: variable `failob2.foo3!int.foo3.p` has undefined state and cannot be read
fail_compilation/failob2.d(119): Error: variable `failob2.foo3!int.foo3.p` is returned but is Undefined
fail_compilation/failob2.d(126): Error: template instance `failob2.foo3!int` error instantiating
---
*/

#line 100

@live
T* foo1(T)()
{
T* p = void;
return p;
}

template foo2(T) {
@live T* foo2() {
T* p = void;
return p;
}
}

@live
template foo3(T) {
T* foo3() {
T* p = void;
return p;
}
}

void test1() {
foo1!int();
foo2!int();
foo3!int();
}

/*
TEST_OUTPUT:
---
fail_compilation/failob2.d(205): Error: variable `failob2.foo4!int.foo4.p` is left dangling at return
fail_compilation/failob2.d(209): Error: template instance `failob2.foo4!int` error instantiating
---
*/

#line 200

void* alloc(size_t);

@live void foo4(T)()
{
auto p = alloc(4);
}

void test2() {
foo4!int();
}

0 comments on commit d4b989f

Please sign in to comment.