Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Mar 14, 2018

To allow alias AggrMember = __traits(getMember, Aggr, "member"); The BasicType grammar must be changed, since it's used by AliasDeclarationY.

BasicType:
    BasicTypeX
    . IdentifierList
    IdentifierList
    Typeof
    Typeof . IdentifierList
    TypeCtor ( Type )
    TypeVector
+    Traits

As a bonus, declarations work with a __traits as type.
Another more limited option would have been to change the grammar of AliasDeclarationY:

AliasDeclarationY:
    Identifier TemplateParametersopt = StorageClassesopt Type
+    Identifier TemplateParametersopt = StorageClassesopt Traits
    Identifier TemplateParametersopt = FunctionLiteral

For now, only Types are handled. A second part will allow this for Symbols.

@ghost ghost requested a review from WalterBright as a code owner March 14, 2018 12:08
@dlang-bot dlang-bot added the Review:WIP Work In Progress - not ready for review or pulling label Mar 14, 2018
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @bbasile! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
7804 enhancement Cannot alias __traits directly

}
else if (strcmp(te.ident.toChars, "getMember") != 0)
{
error("invalid `__traits`, only `getMember` can be aliased and not `%s`",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this really be handled by the parser?

Copy link
Author

@ghost ghost Mar 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It's a semantic thing but it can be handled eagerly.

@AndrejMitrovic
Copy link
Contributor

Nice work on handling such an ancient issue!

Tslice : '@',
Treturn : '@',
Tvector : '@',
TTraits : '@',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be Ttraits to follow the naming conventions.

Copy link
Contributor

@RazvanN7 RazvanN7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool addition! Thanks!

src/dmd/parse.d Outdated
{
AST.TraitsExp te;
if (AST.Expression e = parsePrimaryExp())
te = cast(AST.TraitsExp) e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about :

AST.Traits te = cast(AST.TraitsExp)parsePrimaryExp();

src/dmd/parse.d Outdated
te.ident.toChars);
t = new AST.TypeError;
}
else t = new AST.TypeTraits(loc, te);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brackets for if/else bodies should be consistent (use {} for all or for none).

buf.writestring(t.dstring);
}

override void visit(TypeTraits t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar visit method should be added in the ParseTimeTransitiveVisitor [1] otherwise the tree traversal will be truncated.

[1] https://github.com/dlang/dmd/blob/master/src/dmd/transitivevisitor.d


/**
*/
extern (C++) final class TypeTraits : Type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some documentation for this class : why is it needed, when it is created and what information is kept in each field. Hope that is the intent with the empty comments 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed i planned to do it but forgot to.

@ghost
Copy link
Author

ghost commented Mar 16, 2018

First part, handling of types, is done now. Will start the second soon.

src/dmd/parse.d Outdated
// error already emitted while parsing primary
t = new AST.TypeError;
}
else if (strcmp(te.ident.toChars, "getMember") != 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strcmp must die. Really, everyone, please stop using it! Use if (te.ident == Id.getMember).

Copy link
Contributor

@wilzbach wilzbach Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to approve sth. like #7893 then, s.t. we can start using DStrings everywhere.

result = t.addMod(mtype.mod);
//if (result) printf("TypeTraits found to be %s\n".ptr, result.toChars);
if (result is null)
mtype.error(loc, "`%s` cannot be resolved to a valid type",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely need a test case for this

src/dmd/mtype.d Outdated
Tvector,
Tint128,
Tuns128,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no purpose to blank line

override void visit(TypeTraits t)
{
//printf("TypeBasic::toCBuffer2(t.mod = %d)\n", t.mod);
visit(t.exp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need test case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is tested hdrgen ?

src/dmd/mtype.d Outdated
}

/**
* This type is a shell containing a TraitsExp that can be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is a is completely redundant and can be elided.

@wilzbach
Copy link
Contributor

(@reviewers: don't forget that this requires a PR to dlang.org with the spec update before this can be merged)

src/dmd/mtype.d Outdated
{
this.loc = loc;
this.exp = exp;
super(Ttraits);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super call should go first.

src/dmd/mtype.d Outdated
{
TypeTraits tt;
TraitsExp te = cast(TraitsExp) exp.syntaxCopy();
tt = new TypeTraits(loc, te);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare tt here, not on line 5250

}
}

extern (C++) class TypeTraits : Type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an entry in visitor.d, too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i do so i get a conflict with the entry in transitivevisitor.d

t = parseVector();
break;

case TOK.traits:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also needs entry in isBasicType(), parseMixin(), parseStatement(), parsePrimaryExp() and parseUnaryExp(). Basically, just look at where TOK.typeof_ appears, which is very similar.

@WalterBright
Copy link
Member

WalterBright commented Mar 23, 2018

I approve of the concept, but this needs much work. It'll need 100% test coverage. It also needs a changelog entry, and a spec change.

@JinShil JinShil added Review:Needs Changelog A changelog entry needs to be added to /changelog Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org and removed needs dlang.org PR labels Mar 23, 2018
@ghost
Copy link
Author

ghost commented Apr 18, 2018

I give the hand if someone wants to continue. It's not too hard to finish but i don't want to work on this anymore.

@ghost ghost closed this Apr 18, 2018
@TurkeyMan
Copy link
Contributor

Is there anyone that knows how to kick this across the line, or is this dead? :(

@ghost
Copy link
Author

ghost commented Nov 10, 2018

This was almost finished actually, the second part didn't require to add a new AST node.
Unfortunately i have to open another PR because i have forced pushed.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review:Needs Changelog A changelog entry needs to be added to /changelog Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org Review:WIP Work In Progress - not ready for review or pulling Severity:Enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants