Skip to content

Commit b413cb2

Browse files
committed
Merge branch 'lang-gaps' (a builtin base's ancestors read from a live list: the Linux-only CI failure)
Claude-Session: https://claude.ai/code/session_01HGngAnZqGXKXohBJk5N6AZ
2 parents 7979b11 + d1839b0 commit b413cb2

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎src/lython/emitter/lib/EmitterClasses.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,13 @@ void ModuleEmitter::collectDeclaredAncestors(llvm::StringRef cls,
13161316
llvm::StringRef current = worklist.pop_back_val();
13171317
auto bases = declaredClassBases.find(current);
13181318
if (bases == declaredClassBases.end()) {
1319-
for (const std::string &ancestor :
1320-
llvm::drop_begin(manifestLinearization(current)))
1319+
// ⛔ Not `drop_begin(manifestLinearization(current))` in the loop
1320+
// header: C++17 does not extend the returned vector's life through
1321+
// the adaptor, and the loop read freed strings: a name past the
1322+
// string's inline buffer (libstdc++: every `builtins.*`) was missed,
1323+
// and `str(e)` printed the builtin's text.
1324+
auto linearization = manifestLinearization(current);
1325+
for (const std::string &ancestor : llvm::drop_begin(linearization))
13211326
if (ancestor != "builtins.object")
13221327
out.insert(ancestor);
13231328
continue;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# WHAT: an exception whose class declares `__str__` prints by it when held as
2+
# a builtin base whose name is longer than a short string's inline buffer
3+
# (`ArithmeticError`), caught by a tuple of classes or passed as that base.
4+
#
5+
# WHY THIS IS RUN: which `__str__` runs is the caught object's class, known
6+
# only at run time. The gate that walks a builtin base's ancestors read them
7+
# from a list freed before the walk; a name too long to live inside the
8+
# string's own buffer read back as garbage, and the builtin's text (the
9+
# message given to `__init__`) was printed with no diagnostic.
10+
class Missing(ZeroDivisionError):
11+
pass
12+
13+
14+
class Coded(ZeroDivisionError):
15+
def __init__(self, code: int) -> None:
16+
super().__init__("code " + str(code))
17+
self.code = code
18+
19+
def __str__(self) -> str:
20+
return "Coded[" + str(self.code) + "]"
21+
22+
23+
def describe(k: int) -> str:
24+
try:
25+
if k == 1:
26+
raise Missing("one")
27+
raise Coded(k)
28+
except (Missing, Coded) as e:
29+
return type(e).__name__ + ": " + str(e)
30+
31+
32+
def as_arithmetic(e: ArithmeticError) -> str:
33+
return str(e)
34+
35+
36+
print(describe(1))
37+
print(describe(7))
38+
print(as_arithmetic(Coded(3)))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Missing: one
2+
Coded: Coded[7]
3+
Coded[3]

0 commit comments

Comments
 (0)