D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17108 - Associative array byKeyValue is unsafe
Summary: Associative array byKeyValue is unsafe
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: Lucia Cojocaru
URL:
Keywords: safe
Depends on:
Blocks:
 
Reported: 2017-01-19 14:07 UTC by Jack Stouffer
Modified: 2017-12-18 22:54 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jack Stouffer 2017-01-19 14:07:09 UTC
void main () @safe
{
    int[string] aa;
    foreach (val; aa.byKeyValue)
        ++aa[val.key];
}

/d399/f220.d(4): Error: @safe function 'D main' cannot call @system function 'object.byKeyValue!(int[string], string, int).byKeyValue'
/d399/f220.d(4): Error: @safe function 'D main' cannot call @system function 'object.byKeyValue!(int[string], string, int).byKeyValue.Result.empty'
/d399/f220.d(4): Error: @safe function 'D main' cannot call @system function 'object.byKeyValue!(int[string], string, int).byKeyValue.Result.popFront'
/d399/f220.d(5): Error: @safe function 'D main' cannot call @system function 'object.byKeyValue!(int[string], string, int).byKeyValue.Result.front.Pair.key'
Comment 1 hsteoh 2017-03-09 00:20:55 UTC
Hmph. This is caused by the last line in object.byKeyValue()():

----
/usr/src/d/druntime/import/object.d(2071): Error: cast from int[string] to void* not allowed in safe code
test.d(4): Error: template instance object.byKeyValue!(int[string], string, int) error instantiating
----

Could this be a regression?  I vaguely recall casting to void* used to be allowed, you just can't dereference it unless you're in @trusted land.
Comment 2 hsteoh 2017-03-09 00:25:58 UTC
Bah. Regardless of whether the void* cast is allowed or not, _aaRange() is @system so it's still a no-go.
Comment 3 github-bugzilla 2017-11-06 14:15:13 UTC
Commits pushed to master at https://github.com/dlang/druntime

https://github.com/dlang/druntime/commit/51003a398b77eca1b01138412f2e363c38172878
fix issue 17108 Associative array byKeyValue is unsafe

https://github.com/dlang/druntime/commit/3bf2559ce08d449d8d486a4df3f74343f5b70b33
Merge pull request #1944 from somzzz/make_foreach_byKeyValue_safe

fix issue 17108 Associative array byKeyValue is unsafe
Comment 4 github-bugzilla 2017-12-18 22:54:34 UTC
Commits pushed to stable at https://github.com/dlang/druntime

https://github.com/dlang/druntime/commit/51003a398b77eca1b01138412f2e363c38172878
fix issue 17108 Associative array byKeyValue is unsafe

https://github.com/dlang/druntime/commit/3bf2559ce08d449d8d486a4df3f74343f5b70b33
Merge pull request #1944 from somzzz/make_foreach_byKeyValue_safe