Skip to content

Commit 08c4012

Browse files
authored
sempass2: fix crash with tuple-in-array edge case (#1696)
## Summary Fix the compiler crashing in an edge case where a tuple type used as the element type of an array is not used anywhere. ## Details Synthesizing the hooks for a type embedding a tuple type doesn't, by itself, synthesize the hooks for the tuple type. For array construction expressions, `sempass2` didn't separately pass the the array's element type to `createTypeBoundOps`, leaving the tuple type without hooks (unless synthesized elsewhere), causing the compiler to crash when the hooks were needed (happens when the result of a sub-expression is materialized). Passing the array's element type to `createTypeBoundOps` fixes the issue. Fixes #1695 . <!-- Pull Request(PR) Help Before Merge Ensure: * title reads like a short changelog line entry * code includes tests and is documented * leave the source better than before, but split out big reformats See contributor (guide)[https://nim-works.github.io/nimskull/contributing.html] for details, especially if you're new to this project. Tips that make PRs easier: * for big/impactful changes, start with chat/discussions to refine ideas * refine the pull request message over time; don't have to nail it in one go
1 parent a208966 commit 08c4012

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

compiler/sem/sempass2.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,10 @@ proc track(tracked: PEffects, n: PNode) =
14071407
for i in 0..<n.safeLen:
14081408
track(tracked, n[i])
14091409
objConvCheck(tracked.config, n[i])
1410-
if tracked.owner.kind != skMacro:
1410+
if tracked.owner.kind != skMacro and n.typ != nil:
1411+
# the type might be nil when sempass2 is invoked in a
1412+
# ``compiles`` context
1413+
createTypeBoundOps(tracked, elemType(n.typ), n.info)
14111414
createTypeBoundOps(tracked, n.typ, n.info)
14121415
of nkBracketExpr:
14131416
if optStaticBoundsCheck in tracked.currOptions and n.len == 2:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
discard """
2+
description: '''
3+
Regression test for a compiler crash in the edge case where the tuple type
4+
used as an array element type is not used anywhere else and a temporary of
5+
said tuple type has to be materialized.
6+
'''
7+
joinable: false
8+
"""
9+
10+
type Object = object
11+
12+
proc `=destroy`(x: var Object) =
13+
discard
14+
15+
proc test(): Object = discard
16+
17+
# how the second element expression looks like is irrelevant as long as
18+
# it conditionally raises
19+
var arr = [(Object(),), (test(),)]

0 commit comments

Comments
 (0)