mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 13:10:12 +03:00
Infer template this parameter when calling static method on instance (#14446)
* Infer template this parameter when calling static method on instance Part of Issue 10488 - Allow template this parameter with static functions. * Apply suggestions from code review Co-authored-by: Dennis <dkorpel@users.noreply.github.com> Co-authored-by: Dennis <dkorpel@users.noreply.github.com>
This commit is contained in:
parent
f901fd3609
commit
9777994665
2 changed files with 47 additions and 2 deletions
|
@ -1510,7 +1510,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol
|
|||
}
|
||||
}
|
||||
|
||||
if (toParent().isModule() || (_scope.stc & STC.static_))
|
||||
if (toParent().isModule())
|
||||
tthis = null;
|
||||
if (tthis)
|
||||
{
|
||||
|
@ -1533,7 +1533,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol
|
|||
}
|
||||
|
||||
// Match attributes of tthis against attributes of fd
|
||||
if (fd.type && !fd.isCtorDeclaration())
|
||||
if (fd.type && !fd.isCtorDeclaration() && !(_scope.stc & STC.static_))
|
||||
{
|
||||
StorageClass stc = _scope.stc | fd.storage_class2;
|
||||
// Propagate parent storage class, https://issues.dlang.org/show_bug.cgi?id=5504
|
||||
|
|
45
compiler/test/compilable/statictemplatethis.d
Normal file
45
compiler/test/compilable/statictemplatethis.d
Normal file
|
@ -0,0 +1,45 @@
|
|||
mixin template Constructors(){
|
||||
this(){ }
|
||||
this()immutable{ }
|
||||
this()shared{ }
|
||||
}
|
||||
|
||||
class A {
|
||||
public:
|
||||
static T getInstance(this T)() {
|
||||
return new T();
|
||||
}
|
||||
private:
|
||||
mixin Constructors;
|
||||
}
|
||||
|
||||
class B : A {
|
||||
private:
|
||||
mixin Constructors;
|
||||
}
|
||||
|
||||
void f(){
|
||||
auto a = (new A).getInstance;
|
||||
auto b = (new B).getInstance;
|
||||
static assert(is(typeof(a) == A));
|
||||
static assert(is(typeof(b) == B));
|
||||
|
||||
auto ca = (new immutable A).getInstance;
|
||||
auto sb = (new shared B).getInstance;
|
||||
static assert(is(typeof(ca) == immutable A));
|
||||
static assert(is(typeof(sb) == shared B));
|
||||
}
|
||||
|
||||
// https://issues.dlang.org/show_bug.cgi?id=10488
|
||||
version(none)
|
||||
void g(){
|
||||
auto a = A.getInstance();
|
||||
auto b = B.getInstance();
|
||||
static assert(is(typeof(a)==A));
|
||||
static assert(is(typeof(b)==B));
|
||||
|
||||
auto ai = (immutable(A)).getInstance();
|
||||
auto bs = (shared(B)).getInstance();
|
||||
static assert(is(typeof(ai)==immutable(A)));
|
||||
static assert(is(typeof(bs)==shared(B)));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue