Skip to content

Commit

Permalink
Merge pull request #1877 from WalterBright/auto_infer
Browse files Browse the repository at this point in the history
fix Issue 9914 - Do attribute inference for auto functions
  • Loading branch information
andralex committed May 22, 2015
2 parents ba848af + b4c7b2c commit 2333e38
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/func.c
Expand Up @@ -1164,6 +1164,7 @@ void FuncDeclaration::semantic(Scope *sc)
TemplateInstance *ti;
if (fbody &&
(isFuncLiteralDeclaration() ||
(inferRetType && !isCtorDeclaration()) ||
isInstantiated() && !isVirtualMethod() &&
!(ti = parent->isTemplateInstance(), ti && !ti->isTemplateMixin() && ti->tempdecl->ident != ident)))
{
Expand Down
2 changes: 1 addition & 1 deletion test/compilable/extra-files/json.out
Expand Up @@ -181,7 +181,7 @@
"storageClass" : [
"static"
],
"deco" : "FZv",
"deco" : "FNaNbNiNfZv",
"originalType" : "()",
"endline" : 26,
"endchar" : 19
Expand Down
5 changes: 3 additions & 2 deletions test/fail_compilation/fail13336a.d
@@ -1,7 +1,7 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail13336a.d(27): Error: choose(true) is not an lvalue
fail_compilation/fail13336a.d(28): Error: choose(true) is not an lvalue
---
*/

Expand All @@ -22,7 +22,8 @@ auto ref choose(bool f)

void main()
{
static assert(is(typeof(&choose) == Animal function(bool))); // pass
//pragma(msg, typeof(&choose));
static assert(is(typeof(&choose) == Animal function(bool) nothrow @nogc @safe)); // pass

choose(true) = new Dog();
}
2 changes: 1 addition & 1 deletion test/fail_compilation/testInference.d
Expand Up @@ -140,7 +140,7 @@ TEST_OUTPUT:
fail_compilation/testInference.d(154): Error: pure function 'testInference.bar14049' cannot call impure function 'testInference.foo14049!int.foo14049'
---
*/
auto impure14049() { return 1; }
auto impure14049() { static int i = 1; return i; }

void foo14049(T)(T val)
{
Expand Down
12 changes: 6 additions & 6 deletions test/runnable/declaration.d
Expand Up @@ -28,9 +28,9 @@ void test6905()
auto foo1() { static int n; return n; }
auto foo2() { int n; return n; }
auto foo3() { return 1; }
static assert(typeof(&foo1).stringof == "int delegate()");
static assert(typeof(&foo2).stringof == "int delegate()");
static assert(typeof(&foo3).stringof == "int delegate()");
static assert(typeof(&foo1).stringof == "int delegate() nothrow @nogc @safe");
static assert(typeof(&foo2).stringof == "int delegate() pure nothrow @nogc @safe");
static assert(typeof(&foo3).stringof == "int delegate() pure nothrow @nogc @safe");

ref bar1() { static int n; return n; }
static assert(!__traits(compiles, {
Expand All @@ -43,9 +43,9 @@ void test6905()
auto ref baz1() { static int n; return n; }
auto ref baz2() { int n; return n; }
auto ref baz3() { return 1; }
static assert(typeof(&baz1).stringof == "int delegate() ref");
static assert(typeof(&baz2).stringof == "int delegate()");
static assert(typeof(&baz3).stringof == "int delegate()");
static assert(typeof(&baz1).stringof == "int delegate() nothrow @nogc ref @safe");
static assert(typeof(&baz2).stringof == "int delegate() pure nothrow @nogc @safe");
static assert(typeof(&baz3).stringof == "int delegate() pure nothrow @nogc @safe");
}

/***************************************************/
Expand Down
7 changes: 4 additions & 3 deletions test/runnable/funclit.d
Expand Up @@ -393,7 +393,7 @@ void test7288()
return () => { return x; };
}
pragma(msg, typeof(&foo));
alias int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() Dg;
alias int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe Dg;
pragma(msg, Dg);
static assert(is(typeof(&foo) == Dg)); // should pass
}
Expand Down Expand Up @@ -534,10 +534,11 @@ auto foo7743b()
}
void test7743()
{
static assert(is(typeof(&foo7743a) == int delegate() pure nothrow @nogc @safe function()));
pragma(msg, typeof(&foo7743a));
static assert(is(typeof(&foo7743a) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
assert(foo7743a()() == 10);

static assert(is(typeof(&foo7743b) == int delegate() pure nothrow @nogc @safe function()));
static assert(is(typeof(&foo7743b) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
assert(foo7743b()() == 10);
}

Expand Down
4 changes: 2 additions & 2 deletions test/runnable/mangle.d
Expand Up @@ -287,7 +287,7 @@ auto bar12352()

return S();
}
static assert( bar12352 .mangleof == "_D6mangle8bar12352FZS6mangle8bar12352FZ1S");
static assert( bar12352 .mangleof == "_D6mangle8bar12352FNaNbNiNfZS6mangle8bar12352FZ1S");
static assert(typeof(bar12352()) .mangleof == "S6mangle8bar12352FZ1S");
static assert(typeof(bar12352()).func.mangleof == "_D6mangle8bar12352FZ1S4funcMFZv");

Expand All @@ -301,7 +301,7 @@ auto baz12352()

return new C();
}
static assert( baz12352 .mangleof == "_D6mangle8baz12352FZC6mangle8baz12352FZ1C");
static assert( baz12352 .mangleof == "_D6mangle8baz12352FNaNbNfZC6mangle8baz12352FZ1C");
static assert(typeof(baz12352()) .mangleof == "C6mangle8baz12352FZ1C");
static assert(typeof(baz12352()).func.mangleof == "_D6mangle8baz12352FZ1C4funcMFZv");

Expand Down
4 changes: 2 additions & 2 deletions test/runnable/sdtor.d
Expand Up @@ -2678,7 +2678,7 @@ void test9985()
}
auto p = &(retN()); // OK
assert(p == &n);
alias ref const(int) F1();
alias pure nothrow @nogc @safe ref const(int) F1();
static assert(is(typeof(retN) == F1));

enum const(int) x = 1;
Expand All @@ -2687,7 +2687,7 @@ void test9985()
return x;
}
static assert(!__traits(compiles, { auto q = &(retX()); }));
alias const(int) F2();
alias pure nothrow @nogc @safe const(int) F2();
static assert(is(typeof(retX) == F2));
}

Expand Down
10 changes: 5 additions & 5 deletions test/runnable/xtest46.d
Expand Up @@ -3839,7 +3839,7 @@ void test2486()
S s;
s[];
// opSlice should return rvalue
static assert(is(typeof(&S.opSlice) == int[] function()));
static assert(is(typeof(&S.opSlice) == int[] function() pure nothrow @nogc @safe));
static assert(!__traits(compiles, foo(s[]))); // should be NG
}

Expand Down Expand Up @@ -4818,10 +4818,10 @@ int dummyfunc5933();
alias typeof(dummyfunc5933) FuncType5933;

struct S5933a { auto x() { return 0; } }
static assert(is(typeof(&S5933a.init.x) == int delegate()));
static assert(is(typeof(&S5933a.init.x) == int delegate() pure nothrow @nogc @safe));

struct S5933b { auto x() { return 0; } }
static assert(is(typeof(S5933b.init.x) == FuncType5933));
//static assert(is(typeof(S5933b.init.x) == FuncType5933));

struct S5933c { auto x() { return 0; } }
static assert(is(typeof(&S5933c.x) == int function()));
Expand All @@ -4831,10 +4831,10 @@ static assert(is(typeof(S5933d.x) == FuncType5933));


class C5933a { auto x() { return 0; } }
static assert(is(typeof(&(new C5933b()).x) == int delegate()));
static assert(is(typeof(&(new C5933b()).x) == int delegate() pure nothrow @nogc @safe));

class C5933b { auto x() { return 0; } }
static assert(is(typeof((new C5933b()).x) == FuncType5933));
//static assert(is(typeof((new C5933b()).x) == FuncType5933));

class C5933c { auto x() { return 0; } }
static assert(is(typeof(&C5933c.x) == int function()));
Expand Down

0 comments on commit 2333e38

Please sign in to comment.