Skip to content

Commit

Permalink
Issue 12228 - Deprecate usage of 'this' and 'super' as types
Browse files Browse the repository at this point in the history
  • Loading branch information
JinShil committed May 31, 2018
1 parent c3993f9 commit 3905312
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 30 deletions.
33 changes: 33 additions & 0 deletions changelog/deprecate_this_super_as_types.dd
@@ -0,0 +1,33 @@
Deprecate usage of `this` and `super` as types

Prior to this release, `this` and `super` could be used as both data or types
depending on the context. Starting with this release using `this` or `super`
as a type will result in a compiler error.

---
class C
{
shared(this) x; // Deprecation: Using `this` as a type is deprecated. Use `typeof(this)` instead
}

class D : C
{
shared(super) a; // Deprecation: Using `super` as a type is deprecated. Use `typeof(super)` instead
super b; // Deprecation: Using `super` as a type is deprecated. Use `typeof(super)` instead
}
---

Use `typeof(super)` or `typeof(this)` instead.

---
class C
{
shared(typeof(this)) x;
}

class D : C
{
shared(typeof(super)) a;
typeof(super) b;
}
---
2 changes: 1 addition & 1 deletion src/dmd/apply.d
Expand Up @@ -29,7 +29,7 @@ import dmd.visitor;
*/
private extern (C++) final class PostorderExpressionVisitor : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
StoppableVisitor v;

Expand Down
2 changes: 1 addition & 1 deletion src/dmd/canthrow.d
Expand Up @@ -39,7 +39,7 @@ extern (C++) bool canThrow(Expression e, FuncDeclaration func, bool mustNotThrow
// stop walking if we determine this expression can throw
extern (C++) final class CanThrow : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
FuncDeclaration func;
bool mustNotThrow;

Expand Down
4 changes: 2 additions & 2 deletions src/dmd/delegatize.d
Expand Up @@ -82,7 +82,7 @@ private void lambdaSetParent(Expression e, FuncDeclaration fd)
{
extern (C++) final class LambdaSetParent : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
FuncDeclaration fd;

public:
Expand Down Expand Up @@ -137,7 +137,7 @@ extern (C++) bool lambdaCheckForNestedRef(Expression e, Scope* sc)
{
extern (C++) final class LambdaCheckForNestedRef : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
Scope* sc;
bool result;
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/dinterpret.d
Expand Up @@ -261,7 +261,7 @@ struct CompiledCtfeFunction
{
extern (C++) final class VarWalker : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
CompiledCtfeFunction* ccf;

Expand Down
2 changes: 1 addition & 1 deletion src/dmd/expression.d
Expand Up @@ -7061,7 +7061,7 @@ extern (C++) final class CondExp : BinExp
{
extern (C++) final class DtorVisitor : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
Scope* sc;
CondExp ce;
Expand Down
4 changes: 2 additions & 2 deletions src/dmd/func.d
Expand Up @@ -68,7 +68,7 @@ enum BUILTIN : int
*/
extern (C++) final class NrvoWalker : StatementRewriteWalker
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
FuncDeclaration fd;
Scope* sc;
Expand Down Expand Up @@ -3174,7 +3174,7 @@ extern (C++) final class FuncLiteralDeclaration : FuncDeclaration

extern (C++) final class RetWalker : StatementRewriteWalker
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
Scope* sc;
Type tret;
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/inlinecost.d
Expand Up @@ -287,7 +287,7 @@ public:
{
extern (C++) final class LambdaInlineCost : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
InlineCostVisitor icv;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/nogc.d
Expand Up @@ -29,7 +29,7 @@ import dmd.visitor;
*/
extern (C++) final class NOGCVisitor : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
FuncDeclaration f;
bool err;
Expand Down
5 changes: 0 additions & 5 deletions src/dmd/parse.d
Expand Up @@ -2872,11 +2872,6 @@ final class Parser(AST) : Lexer
}
default:
{
// Deprecated in 2018-04.
// Change to error in 2019-04.
// @@@DEPRECATED_2019-04@@@.
if (token.value == TOK.this_)
deprecation("`this` cannot be used as a parameter type. Use `typeof(this)` instead");
stc = storageClass & (AST.STC.in_ | AST.STC.out_ | AST.STC.ref_ | AST.STC.lazy_);
// if stc is not a power of 2
if (stc & (stc - 1) && !(stc == (AST.STC.in_ | AST.STC.ref_)))
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/sapply.d
Expand Up @@ -27,7 +27,7 @@ import dmd.visitor;
*/
extern (C++) final class PostorderStatementVisitor : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
StoppableVisitor v;

Expand Down
4 changes: 2 additions & 2 deletions src/dmd/sideeffect.d
Expand Up @@ -35,7 +35,7 @@ extern (C++) bool isTrivialExp(Expression e)
{
extern (C++) final class IsTrivialExp : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
extern (D) this()
{
Expand Down Expand Up @@ -68,7 +68,7 @@ extern (C++) bool hasSideEffect(Expression e)
{
extern (C++) final class LambdaHasSideEffect : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
extern (D) this()
{
Expand Down
6 changes: 3 additions & 3 deletions src/dmd/statement.d
Expand Up @@ -176,7 +176,7 @@ extern (C++) abstract class Statement : RootObject
{
extern (C++) final class UsesEH : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
Expand Down Expand Up @@ -215,7 +215,7 @@ extern (C++) abstract class Statement : RootObject
{
extern (C++) final class ComeFrom : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
Expand Down Expand Up @@ -254,7 +254,7 @@ extern (C++) abstract class Statement : RootObject
{
extern (C++) final class HasCode : StoppableVisitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
Expand Down
16 changes: 13 additions & 3 deletions src/dmd/typesem.d
Expand Up @@ -1510,7 +1510,7 @@ extern(C++) Expression getProperty(Type t, const ref Loc loc, Identifier ident,

private extern (C++) final class GetPropertyVisitor : Visitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
Loc loc;
Identifier ident;
int flag;
Expand Down Expand Up @@ -2056,7 +2056,7 @@ extern(C++) void resolve(Type mt, const ref Loc loc, Scope* sc, Expression* pe,

private extern(C++) final class ResolveVisitor : Visitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
Loc loc;
Scope* sc;
Expression* pe;
Expand Down Expand Up @@ -2238,6 +2238,16 @@ private extern(C++) final class ResolveVisitor : Visitor
//printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, mt.toChars());
if ((mt.ident.equals(Id._super) || mt.ident.equals(Id.This)) && !hasThis(sc))
{
// @@@DEPRECATED_v2.086@@@.
if (mt.ident.equals(Id._super))
{
deprecation(mt.loc, "Using `super` as a type is deprecated. Use `typeof(super)` instead");
}
// @@@DEPRECATED_v2.086@@@.
if (mt.ident.equals(Id.This))
{
deprecation(mt.loc, "Using `this` as a type is deprecated. Use `typeof(this)` instead");
}
AggregateDeclaration ad = sc.getStructClassScope();
if (ad)
{
Expand Down Expand Up @@ -2549,7 +2559,7 @@ extern(C++) Expression dotExp(Type mt, Scope* sc, Expression e, Identifier ident

private extern(C++) final class DotExpVisitor : Visitor
{
alias visit = super.visit;
alias visit = typeof(super).visit;
Scope *sc;
Expression e;
Identifier ident;
Expand Down
2 changes: 1 addition & 1 deletion test/compilable/test15785.d
Expand Up @@ -18,5 +18,5 @@ class Derived : Base, IBase2
// IBase2.faz(); // doesn't work yet due to a bug in checkAccess
}

super.T t;
typeof(super).T t;
}
4 changes: 2 additions & 2 deletions test/fail_compilation/fail18228.d
Expand Up @@ -2,8 +2,8 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail18228.d(12): Deprecation: `this` cannot be used as a parameter type. Use `typeof(this)` instead
fail_compilation/fail18228.d(13): Deprecation: `this` cannot be used as a parameter type. Use `typeof(this)` instead
fail_compilation/fail18228.d(12): Deprecation: Using `this` as a type is deprecated. Use `typeof(this)` instead
fail_compilation/fail18228.d(13): Deprecation: Using `this` as a type is deprecated. Use `typeof(this)` instead
---
*/

Expand Down
22 changes: 22 additions & 0 deletions test/fail_compilation/test12228.d
@@ -0,0 +1,22 @@
/*
REQUIRED_ARGS: -de
TEST_OUTPUT:
---
fail_compilation/test12228.d(14): Deprecation: Using `this` as a type is deprecated. Use `typeof(this)` instead
fail_compilation/test12228.d(19): Error: no property `x` for type `object.Object`
fail_compilation/test12228.d(20): Deprecation: Using `super` as a type is deprecated. Use `typeof(super)` instead
fail_compilation/test12228.d(21): Deprecation: Using `super` as a type is deprecated. Use `typeof(super)` instead
---
*/

class C
{
shared(this) x;
}

class D : C
{
alias x = typeof(super).x;
shared(super) a;
super b;
}
4 changes: 2 additions & 2 deletions test/fail_compilation/test13867.d
Expand Up @@ -15,14 +15,14 @@ extern (C++) class Y : Base {
override void blah(){}
}
class Z : Base {
alias blah = super.blah;
alias blah = typeof(super).blah;
override void blah(){}//Error
}
class O : Base {
extern (C++) override void blah(){}
}
extern (C++) class OK : Base {
alias blah = super.blah;
alias blah = typeof(super).blah;
override void blah(){}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fail_compilation/test15785b.d
Expand Up @@ -12,7 +12,7 @@ import imports.test15785;

class Derived : Base, IBase2
{
super.T t;
typeof(super).T t;
Base.T t2;
IBase2.T t3;
}

0 comments on commit 3905312

Please sign in to comment.