Bug 23678 – Contracts are compiled with release switch?

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-02-07T19:55:44Z
Last change time
2023-02-09T20:29:26Z
Assigned to
No Owner
Creator
Tomáš Chaloupka

Comments

Comment #0 by chalucha — 2023-02-07T19:55:44Z
Test case: ```D version (assert) int foo; void test() in (!foo) {} void main() { test(); } ``` When built with `-release`, `foo` is not compiled in and compilation fails with: ``` Error: undefined identifier `foo` ``` Should't the contract be dropped too in release builds?
Comment #1 by razvan.nitu1305 — 2023-02-09T15:21:38Z
The contract is dropped (i.e. it does not appear in the executable file), but that does not mean it is not analyzed. You are getting an error because your code does not pass semantic analysis. We cannot implement this because then you might have semantically incorrect code in your contract and the compiler needs to report it regardless of the fact that it does not get generated in the object code.
Comment #2 by dkorpel — 2023-02-09T20:29:26Z
> We cannot implement this because then you might have semantically incorrect code in your contract and the compiler needs to report it regardless of the fact that it does not get generated in the object code It's not set in stone that the compiler has to report semantic errors in contracts. Note that currently, you can have semantically incorrect code in unittest blocks if you don't compile with -unittest. (In fact, it doesn't even have to parse correctly, the parser just skips to the next bracket) However, contracts can affect attribute inference: a `@system` contract will make calling that function `@system`. Changing function attributes based on -checkaction switches can result in linker errors with separate compilation.