Skip to content

Commit 05948f1

Browse files
authored
fix: improve precision of synthesis failure spans in interpolated strings (#9004)
This PR ensures that type-class synthesis failure errors in interpolated strings are displayed at the interpolant at which they occurred.
1 parent 6b520ed commit 05948f1

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/Init/Meta.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ def expandInterpolatedStrChunks (chunks : Array Syntax) (mkAppend : Syntax → S
14301430
let mut i := 0
14311431
let mut result := Syntax.missing
14321432
for elem in chunks do
1433-
let elem ← match elem.isInterpolatedStrLit? with
1433+
let elem ← withRef elem <| match elem.isInterpolatedStrLit? with
14341434
| none => mkElem elem
14351435
| some str => mkElem (Syntax.mkStrLit str)
14361436
if i == 0 then
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/-!
2+
# String interpolation synthesis diagnostic spans
3+
4+
The spans for type-class synthesis errors in interpolated strings to be localized to the
5+
interpolants at which synthesis failure occurs.
6+
-/
7+
8+
structure Foo where
9+
structure Bar where
10+
11+
def foo := Foo.mk
12+
def bar := Bar.mk
13+
14+
instance : ToString Foo := ⟨fun _ => "foo"
15+
16+
example := s!"a {foo} b {bar} c"
17+
--^ collectDiagnostics
18+
--^ collectDiagnostics
19+
--^ collectDiagnostics
20+
21+
example := s!"{bar} a"
22+
--^ collectDiagnostics
23+
--^ collectDiagnostics
24+
--^ collectDiagnostics
25+
26+
27+
example := s!"a {bar}"
28+
--^ collectDiagnostics
29+
--^ collectDiagnostics
30+
--^ collectDiagnostics
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{"version": 1,
2+
"uri": "file:///strInterpSynthError.lean",
3+
"diagnostics":
4+
[{"source": "Lean 4",
5+
"severity": 1,
6+
"range":
7+
{"start": {"line": 15, "character": 25},
8+
"end": {"line": 15, "character": 28}},
9+
"message":
10+
"failed to synthesize\n ToString Bar\n\nAdditional diagnostic information may be available using the `set_option diagnostics true` command.",
11+
"fullRange":
12+
{"start": {"line": 15, "character": 25},
13+
"end": {"line": 15, "character": 28}}},
14+
{"source": "Lean 4",
15+
"severity": 1,
16+
"range":
17+
{"start": {"line": 20, "character": 15},
18+
"end": {"line": 20, "character": 18}},
19+
"message":
20+
"failed to synthesize\n ToString Bar\n\nAdditional diagnostic information may be available using the `set_option diagnostics true` command.",
21+
"fullRange":
22+
{"start": {"line": 20, "character": 15},
23+
"end": {"line": 20, "character": 18}}},
24+
{"source": "Lean 4",
25+
"severity": 1,
26+
"range":
27+
{"start": {"line": 26, "character": 17},
28+
"end": {"line": 26, "character": 20}},
29+
"message":
30+
"failed to synthesize\n ToString Bar\n\nAdditional diagnostic information may be available using the `set_option diagnostics true` command.",
31+
"fullRange":
32+
{"start": {"line": 26, "character": 17},
33+
"end": {"line": 26, "character": 20}}}]}

0 commit comments

Comments
 (0)