-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incorrect code generation for parameter borrowing (#1460)
## Summary Fix parameters below the pass-by-reference size threshold not being passed by reference when the procedure returns a non-direct view, resulting in access violations at run-time when trying to access the returned view. ## Details * consider immutable non-direct views (e.g., `object` types with `lent` fields) when deciding whether pass-by-reference is used for the first parameter of a procedure * move the `classifyViewType` procedures from `typeallowed` to `types`, as they're wholly unrelated to the "type allowed" checks Fixes #1457.
- Loading branch information
Showing
5 changed files
with
103 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,9 +39,6 @@ import | |
options, | ||
msgs, | ||
], | ||
compiler/sem/[ | ||
typeallowed, | ||
], | ||
compiler/modules/[ | ||
modulegraphs, | ||
] | ||
|
27 changes: 27 additions & 0 deletions
27
tests/lang_experimental/views/timmutable_borrow_from_first_parameter.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
discard """ | ||
description: ''' | ||
Ensure a view borrowing from the first parameter can be safely returned | ||
from a procedure. | ||
''' | ||
targets: c js vm | ||
knownIssue.js vm: "The first parameter isn't passed by reference" | ||
""" | ||
|
||
block direct_lent_view_primitive: | ||
# test borrowing from primitive-type parameter with a direct view | ||
proc test(x: int): lent int = | ||
x | ||
|
||
var x = 0 | ||
doAssert addr(test(x)) == addr(x) | ||
|
||
block object_lent_view_primitive: | ||
# test borrowing from primitive-type parameter with an indirect view | ||
type Object = object | ||
x: lent int | ||
|
||
proc test(x: int): Object = | ||
Object(x: x) | ||
|
||
var x = 0 | ||
doAssert addr(test(x).x) == addr(x) |