Skip to content

Add location to user defined attributes#9359

Closed
jacob-carlborg wants to merge 2 commits intodlang:masterfrom
jacob-carlborg:uda-loc
Closed

Add location to user defined attributes#9359
jacob-carlborg wants to merge 2 commits intodlang:masterfrom
jacob-carlborg:uda-loc

Conversation

@jacob-carlborg
Copy link
Contributor

All symbols should have location information. This is important especially when the compiler is used as a library.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @jacob-carlborg! 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

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#9359"

@jacob-carlborg
Copy link
Contributor Author

jacob-carlborg commented Feb 13, 2019

Depends on: #9350. #9350 has been merged.

@jacob-carlborg jacob-carlborg added Review:WIP Work In Progress - not ready for review or pulling and removed Merge:Blocked labels Feb 13, 2019
@PetarKirov
Copy link
Member

Needs a rebase, otherwise looking good.

@jacob-carlborg
Copy link
Contributor Author

This needs more work.

@jacob-carlborg jacob-carlborg removed the Review:WIP Work In Progress - not ready for review or pulling label Feb 14, 2019
@jacob-carlborg jacob-carlborg force-pushed the uda-loc branch 3 times, most recently from 4ac65f5 to 812f3cd Compare February 14, 2019 19:32
@jacob-carlborg
Copy link
Contributor Author

I think this is ready to go now. I turned out it required a bit more code then I initially thought. My original tests were flawed. Now all the tests have the UDA own its line to make sure it's the actual location of the UDA AST node that is used and not the AST node of the declaration.

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

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

How does this handle multiple UDAs?

@jacob-carlborg
Copy link
Contributor Author

How does this handle multiple UDAs?

Hmm, that's a good question. I'll add a test to find out.

* Parse const/immutable/shared/inout/nothrow/pure postfix
*/
StorageClass parsePostfix(StorageClass storageClass, AST.Expressions** pudas)
StorageClass parsePostfix(StorageClass storageClass, AST.Expressions** pudas, Loc* udaLoc)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to use * rather than ref?

Copy link
Member

Choose a reason for hiding this comment

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

Or out ?

Copy link
Contributor

Choose a reason for hiding this comment

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

used with null here

Copy link
Member

Choose a reason for hiding this comment

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

It's not actually relying on it being null. Supplying a temporary for the parameter will work fine, and it will remove the null dependence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, using an out parameter would require to declare a variable at the call site, which would not be used afterwards in a couple of cases.

Copy link
Member

Choose a reason for hiding this comment

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

That is correct. However, on the plus side:

  1. It reduces the complexity of the code by eliminating the conditional logic.
  2. It eliminates use of a pointer, thereby eliminating potential memory corruption.
  3. It eliminates the need to document that the argument may be null.
  4. It eliminates the need for the user to check that there's a null check on every dereference on the pointer.
  5. out self-documents that this will be initialized by the function, not the caller.
  6. The compiler optimizer knows (5) too, though currently it does not make use of such information.

Hence it is a better practice to use out.


case TOK.at:
{
if (udaLoc)
Copy link
Member

Choose a reason for hiding this comment

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

Avoid null check by using out parameter.

All symbols should have location information. This is important
especially when the compiler is used as a library.
@jacob-carlborg jacob-carlborg added the Review:WIP Work In Progress - not ready for review or pulling label Feb 17, 2019
@jacob-carlborg
Copy link
Contributor Author

How does this handle multiple UDAs?

This seems a bit more complicated.

@jacob-carlborg
Copy link
Contributor Author

jacob-carlborg commented Feb 18, 2019

How does this handle multiple UDAs?

This seems a bit more complicated.

As far as I can see the parse will combine multiple UDA declarations in the source code into a single AST node, UserAttributeDeclaration. Currently for a test with multiple UDAs on multiple lines, I'll get the line number of the last UDA. There is no AST node to attach the location to. The expressions inside the UDA will contain location information but that will start where the expression start, that is, it will be missing the @( from the UDA. I think to preserve the location information the AST need to look different.

@thewilsonator @WalterBright any advice?

@RazvanN7
Copy link
Contributor

Superseeded by: #12524

@RazvanN7 RazvanN7 closed this Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review:Needs Rebase Review:Needs Work Review:stalled Review:WIP Work In Progress - not ready for review or pulling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants