-
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.
Report error for foreign forward declared generic (#1374)
## Summary Fix the compiler or backend crashing when using an incomplete generic procedure from another module. A proper compiler error is now reported in this case. Fixes #1368. ## Details The issues can only occur in the presence of cyclic imports. Instantiating a generic routine registers it in the instantiating module, preventing its body from being patched when the generic routine is complete. Using the instantiation cache when completing a forward- declared generic won't work, since the module an instantiation comes from might be closed already. Instead, this situation is now detected and an error is reported. No error is reported if no instantiation takes place (i.e., because the instantiation was cached already), meaning that previously working code stays working.
- Loading branch information
Showing
5 changed files
with
35 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import tgeneric_cycle_forward | ||
|
||
forwarded[int]() # was already instantiated; works | ||
# try to instantiate the incomplete generic routine: | ||
forwarded[float]() |
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,20 @@ | ||
discard """ | ||
description: ''' | ||
Ensure instantiating foreign, incomplete generic procedures leads to a | ||
proper error. | ||
''' | ||
errormsg: "cannot instantiate generic procedure forward-declared in another module" | ||
file: "mgeneric_cycle_forward.nim" | ||
line: 5 | ||
""" | ||
|
||
proc forwarded*[T]() | ||
|
||
# instantiate with `int` before starting the import cycle | ||
forwarded[int]() | ||
|
||
import mgeneric_cycle_forward # start the recursive import | ||
|
||
# complete the forward declaration: | ||
proc forwarded[T]() = | ||
discard |