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

Implement Safer D #17044

Merged
merged 1 commit into from
Nov 28, 2024
Merged

Implement Safer D #17044

merged 1 commit into from
Nov 28, 2024

Conversation

WalterBright
Copy link
Member

DIP: https://github.com/WalterBright/documents/blob/38f0a846726b571f8108f6e63e5e217b91421c86/safer.md

This adds -preview=safer which enables safety checking on un-attributed functions. This just makes one check for starters.

Newsgroup:
https://www.digitalmars.com/d/archives/digitalmars/dip/development/Safer_D_first_draft_387.html

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

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

@thewilsonator thewilsonator added Needs Changelog A changelog entry needs to be added to /changelog Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org labels Nov 1, 2024
@WalterBright WalterBright removed the Needs Changelog A changelog entry needs to be added to /changelog label Nov 26, 2024
@WalterBright WalterBright changed the title add nascent implementation of Safer D Implement Safer D Nov 27, 2024
@WalterBright
Copy link
Member Author

Finished the implementation. Turned out to be much less code than I anticipated.

@WalterBright
Copy link
Member Author

It doesn't need a spec PR because it's behind a -preview switch.

bool isSaferD(FuncDeclaration fd)
{
return fd.type.toTypeFunction().trust == TRUST.default_ &&
global.params.safer == FeatureState.enabled;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer this checking a scope flag rather than a global variable

Copy link
Member Author

Choose a reason for hiding this comment

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

You are not wrong, but it would require more disruption as the flag isn't passed along. I suppose it would be alright for a future refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

This will likely be limited to newer editions, so it'll have to check it at the module level. Which eliminates the global flag.

@WalterBright WalterBright merged commit 347c883 into dlang:master Nov 28, 2024
41 checks passed
@WalterBright WalterBright deleted the safer branch November 28, 2024 06:59
@vnayar
Copy link

vnayar commented Nov 28, 2024

This is a cool idea! It would also mean you can compile projects that depend on libraries that require @safe and make them work with other libraries that do not use an explicit @safe. That incompatibility creates a lot of friction, and the ability to automatically combine code that de-facto is safe would remove a lot of the hesitancy around using the @safe feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants