Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make safe error messages consistent #20654

Merged
merged 1 commit into from
Jan 8, 2025

Conversation

dkorpel
Copy link
Contributor

@dkorpel dkorpel commented Jan 8, 2025

There are quite a few safety error messages, and some tell they are related to 'safe code' or @safe functions, while others don't. This is not only inconsistent, it's also false with -preview=safer: some of these errors occur in functions without a @safe annotation.

This PR rewrites all safety error messages so they either form a noun or gerund: "pointer arithmetic" or "void initializing a pointer". The error reporter will then surround that in context:

deprecations: "{action} will become @system in the future"
safe functions: "{action} is not allowed in a @safe function"
safer functions: "{action} is not allowed in a function with default safety with -preview=safer"
system variables: "{action} can't initialize @safe variable %s"
inferred supplemental error: "which wasn't safe because of: {action}"

Occasionally this requirement results in suggested fixes being phrased slightly awkwardly:

Error: using `b.ptr` (instead of `&b[0])` is not allowed in a `@safe` function

A future enhancement of adding supplemental errors to setUnsafe could improve this.

@dlang-bot
Copy link
Contributor

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.

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#20654"

@dkorpel dkorpel marked this pull request as ready for review January 8, 2025 03:27
@thewilsonator thewilsonator merged commit 9b94878 into dlang:master Jan 8, 2025
41 checks passed
Comment on lines -1012 to +1013
"copying `%s` into allocated memory escapes a reference to parameter `%s`" :
"copying `%s` into allocated memory escapes a reference to local variable `%s`";
return setUnsafePreview(&sc, fs, gag, e.loc, msg, e, v);
"escaping a reference to parameter `%s` by copying `%s` into allocated memory" :
"escaping a reference to local variable `%s by copying `%s` into allocated memory";
Copy link
Member

@ibuclaw ibuclaw Jan 10, 2025

Choose a reason for hiding this comment

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

Hi @dkorpel - this caused a diagnostic regression - there's a missing closing backtick in this format string.

Are you able to fix this? Alternatively can include it in the bundle of "broken DMD/C++ API" things I'm preparing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, give me a minute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dkorpel dkorpel deleted the safer-error-msg branch January 10, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants