Skip to content

Commit f1fe5c0

Browse files
authored
refactor: Debug implementation of TextLocation (#1457)
Changes the default debug format of `TextLocation` to `Range(<start line>:<start column> - <end line>:<end column>`
1 parent 4385631 commit f1fe5c0

File tree

57 files changed

+318
-3004
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+318
-3004
lines changed

compiler/plc_lowering/src/tests/inheritance_tests.rs

Lines changed: 27 additions & 297 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
---
2-
source: compiler/plc_source/./src/source_location.rs
2+
source: compiler/plc_source/src/source_location.rs
33
expression: loc1.span(&loc2)
44
---
55
SourceLocation {
6-
span: Range(
7-
TextLocation {
8-
line: 1,
9-
column: 0,
10-
offset: 0,
11-
}..TextLocation {
12-
line: 5,
13-
column: 30,
14-
offset: 10,
15-
},
16-
),
6+
span: Range(1:0 - 5:30),
177
}

compiler/plc_source/src/source_location.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl TextLocation {
7676
}
7777

7878
/// Represents the location of a code element in a source code
79-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79+
#[derive(Clone, PartialEq, Eq, Hash)]
8080
pub enum CodeSpan {
8181
/// The location of a block in a diagram
8282
Block { local_id: usize, execution_order: Option<usize>, inner_range: Option<Range<usize>> },
@@ -88,6 +88,31 @@ pub enum CodeSpan {
8888
None,
8989
}
9090

91+
impl std::fmt::Debug for CodeSpan {
92+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93+
match self {
94+
Self::Block { local_id, execution_order, inner_range } => f
95+
.debug_struct("Block")
96+
.field("local_id", local_id)
97+
.field("execution_order", execution_order)
98+
.field("inner_range", inner_range)
99+
.finish(),
100+
Self::Combined(arg0) => f.debug_tuple("Combined").field(arg0).finish(),
101+
Self::None => write!(f, "None"),
102+
103+
// The default debug implementation for Ranges is very verbose, which makes reading the output
104+
// of `--ast` or snapshots "harder" than it should be. Thus we do it in a more concise way here
105+
Self::Range(range) => {
106+
write!(
107+
f,
108+
"Range({}:{} - {}:{})",
109+
range.start.line, range.start.column, range.end.line, range.end.column
110+
)
111+
}
112+
}
113+
}
114+
}
115+
91116
impl Display for CodeSpan {
92117
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93118
match self {

src/index/tests/index_tests.rs

Lines changed: 10 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,35 +1747,15 @@ fn aliased_hardware_access_variable_has_implicit_initial_value_declaration() {
17471747
},
17481748
],
17491749
location: SourceLocation {
1750-
span: Range(
1751-
TextLocation {
1752-
line: 2,
1753-
column: 16,
1754-
offset: 40,
1755-
}..TextLocation {
1756-
line: 2,
1757-
column: 29,
1758-
offset: 53,
1759-
},
1760-
),
1750+
span: Range(2:16 - 2:29),
17611751
file: Some(
17621752
"<internal>",
17631753
),
17641754
},
17651755
},
17661756
),
17671757
source_location: SourceLocation {
1768-
span: Range(
1769-
TextLocation {
1770-
line: 2,
1771-
column: 12,
1772-
offset: 36,
1773-
}..TextLocation {
1774-
line: 2,
1775-
column: 15,
1776-
offset: 39,
1777-
},
1778-
),
1758+
span: Range(2:12 - 2:15),
17791759
file: Some(
17801760
"<internal>",
17811761
),
@@ -1813,17 +1793,7 @@ fn aliased_hardware_access_variable_creates_global_var_for_address() {
18131793
linkage: Internal,
18141794
binding: None,
18151795
source_location: SourceLocation {
1816-
span: Range(
1817-
TextLocation {
1818-
line: 2,
1819-
column: 16,
1820-
offset: 40,
1821-
}..TextLocation {
1822-
line: 2,
1823-
column: 29,
1824-
offset: 53,
1825-
},
1826-
),
1796+
span: Range(2:16 - 2:29),
18271797
file: Some(
18281798
"<internal>",
18291799
),
@@ -1846,17 +1816,7 @@ fn aliased_hardware_access_variable_creates_global_var_for_address() {
18461816
},
18471817
nature: Any,
18481818
location: SourceLocation {
1849-
span: Range(
1850-
TextLocation {
1851-
line: 2,
1852-
column: 30,
1853-
offset: 54,
1854-
}..TextLocation {
1855-
line: 2,
1856-
column: 36,
1857-
offset: 60,
1858-
},
1859-
),
1819+
span: Range(2:30 - 2:36),
18601820
file: Some(
18611821
"<internal>",
18621822
),
@@ -1924,17 +1884,7 @@ fn aliased_hardware_access_variable_is_indexed_as_a_pointer() {
19241884
},
19251885
nature: Any,
19261886
location: SourceLocation {
1927-
span: Range(
1928-
TextLocation {
1929-
line: 2,
1930-
column: 30,
1931-
offset: 54,
1932-
}..TextLocation {
1933-
line: 2,
1934-
column: 36,
1935-
offset: 60,
1936-
},
1937-
),
1887+
span: Range(2:30 - 2:36),
19381888
file: Some(
19391889
"<internal>",
19401890
),
@@ -1974,17 +1924,7 @@ fn address_used_in_2_aliases_only_created_once() {
19741924
linkage: Internal,
19751925
binding: None,
19761926
source_location: SourceLocation {
1977-
span: Range(
1978-
TextLocation {
1979-
line: 2,
1980-
column: 16,
1981-
offset: 40,
1982-
}..TextLocation {
1983-
line: 2,
1984-
column: 29,
1985-
offset: 53,
1986-
},
1987-
),
1927+
span: Range(2:16 - 2:29),
19881928
file: Some(
19891929
"<internal>",
19901930
),
@@ -2027,17 +1967,7 @@ fn aliased_variable_with_in_or_out_directions_create_the_same_variable() {
20271967
linkage: Internal,
20281968
binding: None,
20291969
source_location: SourceLocation {
2030-
span: Range(
2031-
TextLocation {
2032-
line: 2,
2033-
column: 16,
2034-
offset: 40,
2035-
}..TextLocation {
2036-
line: 2,
2037-
column: 29,
2038-
offset: 53,
2039-
},
2040-
),
1970+
span: Range(2:16 - 2:29),
20411971
file: Some(
20421972
"<internal>",
20431973
),
@@ -2062,17 +1992,7 @@ fn aliased_variable_with_in_or_out_directions_create_the_same_variable() {
20621992
linkage: Internal,
20631993
binding: None,
20641994
source_location: SourceLocation {
2065-
span: Range(
2066-
TextLocation {
2067-
line: 4,
2068-
column: 17,
2069-
offset: 117,
2070-
}..TextLocation {
2071-
line: 4,
2072-
column: 30,
2073-
offset: 130,
2074-
},
2075-
),
1995+
span: Range(4:17 - 4:30),
20761996
file: Some(
20771997
"<internal>",
20781998
),
@@ -2113,17 +2033,7 @@ fn if_two_aliased_var_of_different_types_use_the_same_address_the_first_wins() {
21132033
linkage: Internal,
21142034
binding: None,
21152035
source_location: SourceLocation {
2116-
span: Range(
2117-
TextLocation {
2118-
line: 2,
2119-
column: 16,
2120-
offset: 40,
2121-
}..TextLocation {
2122-
line: 2,
2123-
column: 29,
2124-
offset: 53,
2125-
},
2126-
),
2036+
span: Range(2:16 - 2:29),
21272037
file: Some(
21282038
"<internal>",
21292039
),
@@ -2161,17 +2071,7 @@ fn var_config_hardware_address_creates_global_variable() {
21612071
linkage: Internal,
21622072
binding: None,
21632073
source_location: SourceLocation {
2164-
span: Range(
2165-
TextLocation {
2166-
line: 2,
2167-
column: 24,
2168-
offset: 48,
2169-
}..TextLocation {
2170-
line: 2,
2171-
column: 37,
2172-
offset: 61,
2173-
},
2174-
),
2074+
span: Range(2:24 - 2:37),
21752075
file: Some(
21762076
"<internal>",
21772077
),

0 commit comments

Comments
 (0)