Issue 3841 - silent implicit cast from floating point to integral in += etc. operators
Summary: silent implicit cast from floating point to integral in += etc. operators
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 major
Assignee: No Owner
URL:
Keywords: accepts-invalid
: 17600 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-02-22 10:28 UTC by Andrei Alexandrescu
Modified: 2017-07-19 17:43 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2010-02-22 10:28:45 UTC
void main() {
    uint a;
    float b = 0.1;
    a += b;
}

This program compiles no problem. This is obviously a problem and a very surprising behavior; I'm at the end of a 2-hours bug chasing and tried this only after I eliminated all other opportunities for error.

For numeric types, It is ok for built-in += to perform narrowing conversion, but not truncating ones.
Comment 1 Andrei Alexandrescu 2010-04-27 11:53:59 UTC
Hell yeah it is major. Thanks Don.
Comment 2 Clement Courbet 2015-05-10 19:10:02 UTC
pull https://github.com/D-Programming-Language/dmd/pull/4643
Comment 3 Clement Courbet 2015-05-13 18:23:39 UTC
pull https://github.com/D-Programming-Language/phobos/pull/3278
Comment 4 github-bugzilla 2015-05-14 01:55:21 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8558b22cfe818b48c6b5efdb1a0439cf6c7e9fa7
Remove a bad cast (issue 3841). Functional noop.

https://github.com/D-Programming-Language/phobos/commit/4ba5fc4d581e577eb09c1ccf8604f40a02083651
Merge pull request #3278 from legrosbuffle/fix3841_phobos

Remove a bad cast (Required for fixing issue 3841). Functional noop.
Comment 5 github-bugzilla 2015-05-14 11:04:04 UTC
Commits pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/0109acf3a2452580e354fab182dd148d5f921d3d
Avoid silent truncating conversions (issue 3841).

https://github.com/D-Programming-Language/druntime/commit/254c8ee9371af2cd85a6f74fcfd583a370d31254
Merge pull request #1268 from legrosbuffle/explicit-truncating-conversions

Avoid silent truncating conversions (issue 3841).
Comment 6 github-bugzilla 2015-05-19 17:25:48 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/bd259864438f9150d0eac592252978c48c09d533
Merge pull request #4643 from legrosbuffle/fix3841

Issue 3841: silent implicit cast from floating point to integral in += etc. operators
Comment 7 github-bugzilla 2015-06-17 21:02:44 UTC
Commits pushed to stable at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/0109acf3a2452580e354fab182dd148d5f921d3d
Avoid silent truncating conversions (issue 3841).

https://github.com/D-Programming-Language/druntime/commit/254c8ee9371af2cd85a6f74fcfd583a370d31254
Merge pull request #1268 from legrosbuffle/explicit-truncating-conversions
Comment 8 github-bugzilla 2015-06-17 21:05:18 UTC
Commit pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/bd259864438f9150d0eac592252978c48c09d533
Merge pull request #4643 from legrosbuffle/fix3841
Comment 9 Vladimir Panteleev 2017-07-05 12:57:21 UTC
*** Issue 17600 has been marked as a duplicate of this issue. ***
Comment 10 github-bugzilla 2017-07-19 17:41:38 UTC
Commits pushed to dmd-cxx at https://github.com/dlang/druntime

https://github.com/dlang/druntime/commit/0109acf3a2452580e354fab182dd148d5f921d3d
Avoid silent truncating conversions (issue 3841).

https://github.com/dlang/druntime/commit/254c8ee9371af2cd85a6f74fcfd583a370d31254
Merge pull request #1268 from legrosbuffle/explicit-truncating-conversions
Comment 11 github-bugzilla 2017-07-19 17:43:20 UTC
Commits pushed to dmd-cxx at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/8558b22cfe818b48c6b5efdb1a0439cf6c7e9fa7
Remove a bad cast (issue 3841). Functional noop.

https://github.com/dlang/phobos/commit/4ba5fc4d581e577eb09c1ccf8604f40a02083651
Merge pull request #3278 from legrosbuffle/fix3841_phobos