Skip to content

Fix #18670 - Default arguments bypass most attribute checks - #22908

Open
dkorpel wants to merge 4 commits into
dlang:masterfrom
dkorpel:defaultarg-attributes
Open

dkorpel wants to merge 4 commits into
dlang:masterfrom
dkorpel:defaultarg-attributes

Conversation

@dkorpel

@dkorpel dkorpel commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes #18670

@dlang-bot

Copy link
Copy Markdown

Thanks for your pull request and interest in making D better, @dkorpel! 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 coverage diff by visiting the details link of the codecov check)
  • 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.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

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

dub run digger -- build "master + dmd#22908"

Comment on lines +3314 to +3319
.deprecation(loc, "`pure` %s `%s` accessing mutable static data `%s` in default argument",
sc.func.kind(), sc.func.toPrettyChars(), v.toErrMsg());
else
{
error(loc, "`pure` %s `%s` cannot access mutable static data `%s`",
sc.func.kind(), sc.func.toPrettyChars(), v.toErrMsg());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

any reason not to use previewErrorFunc here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm trying to get something basic working here before making it nice and tidy. The more pressing issue currently is that void defaultUnsafe2(int x = globalPtr[1]) @safe still gives an Error instead of a Deprecation. I'll mark it as ready for review and ping once it's in a good state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried it, it gets a bit messy with the message being different in the two branches. I think 2 statements is clearer.

{
if (useDeprecation)
{
.deprecation(loc, "`pure` %s `%s` calling impure %s `%s` in default argument",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

Comment thread compiler/src/dmd/expressionsem.d Resolved
if (useDeprecation)
{
// Introduced in 2.113
.deprecation(loc, "`@safe` %s `%s` calling `@system` %s `%s` in default argument",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

dkorpel and others added 4 commits July 10, 2026 13:59
Direct memory-safety violations (e.g. indexing a pointer) go through
sc.setUnsafe rather than the function-call checkSafety path, so they
still emitted a hard error in default arguments. Emit a deprecation
there too, consistent with the other default-argument attribute checks.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@dkorpel
dkorpel force-pushed the defaultarg-attributes branch from 43cdeb5 to 5ea0b21 Compare July 10, 2026 12:00
@dkorpel
dkorpel marked this pull request as ready for review July 10, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default arguments bypass most attributes check (pure, @safe, @nogc)

3 participants