Universal Function Attribute Inference

Paul Backus snarwin at gmail.com
Wed Feb 28 17:18:04 UTC 2024


### Description

Currently, function attributes and function parameter attributes 
are only inferred by the D compiler for certain kinds of 
functions. This DIP idea proposes that such inference be extended 
to all non-overridable functions with bodies.

The primary goal of universal inference is to solve D's 
"attribute soup" problem without breaking compatibility with 
existing code. Compatibility with existing code makes universal 
inference a better solution to this problem than "@safe by 
default," "nothrow by default," and other similar proposals.

Overridable functions (that is, non-final virtual functions) are 
excluded from universal inference because their bodies may be 
replaced at runtime.

For cases where attribute inference is not desired, an opt-out 
mechanism will be provided.

Currently, `.di` files generated by the compiler do not include 
inferred function attributes. This will have to change.

### Related Links

* [Discussion of inference pros and cons][andrei-comment] by 
Andrei Alexandrescu
* [Thoughts on inferred attributes][adr-post] by Adam Ruppe
* [DIP70: @api/extern(noinfer) attribute][dip70]
* [Add `@default` attribute][at-default]

[andrei-comment]: 
https://github.com/dlang/dmd/pull/1877#issuecomment-16403663
[adr-post]: 
http://dpldocs.info/this-week-in-d/Blog.Posted_2022_07_11.html#inferred-attributes
[dip70]: https://wiki.dlang.org/DIP70
[at-default]: https://github.com/dlang/DIPs/pull/236


More information about the dip.ideas mailing list