Skip to content

Commit 25b5afb

Browse files
committed
more tests, more validation, some cleanups
1 parent 7fa0fca commit 25b5afb

File tree

6 files changed

+180
-248
lines changed

6 files changed

+180
-248
lines changed

src/codegen/generators/expression_generator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,14 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
220220
AstStatement::This => {
221221
// TODO: #THIS meaningful errors
222222
// [ ] add tests
223-
let function_context = self.function_context.ok_or_else(|| {
223+
self.function_context.ok_or_else(|| {
224224
Diagnostic::codegen_error("Cannot use 'this' without context", expression)
225225
})?;
226-
let this_name = self.annotations.get_call_name(expression).unwrap();
227226
let Some(this_name) = self.annotations.get_call_name(expression) else {
228227
todo!("error handling")
229228
};
230229
let this_value =
231-
self.llvm_index.find_loaded_associated_variable_value(&this_name).ok_or_else(|| {
230+
self.llvm_index.find_loaded_associated_variable_value(this_name).ok_or_else(|| {
232231
let message = format!("Cannot find '{}' in associated variable values", this_name);
233232
Diagnostic::codegen_error(message, expression)
234233
})?;

src/parser/tests/expressions_parser_tests.rs

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,32 +2019,6 @@ fn this_keyword_can_be_mixed_with_super() {
20192019
let parse_result = parse(src).0;
20202020
assert_debug_snapshot!(parse_result.implementations[0].statements, @r#"
20212021
[
2022-
ReferenceExpr {
2023-
kind: Member(
2024-
Identifier {
2025-
name: "x",
2026-
},
2027-
),
2028-
base: Some(
2029-
This,
2030-
),
2031-
},
2032-
ReferenceExpr {
2033-
kind: Member(
2034-
Identifier {
2035-
name: "y",
2036-
},
2037-
),
2038-
base: Some(
2039-
ReferenceExpr {
2040-
kind: Deref,
2041-
base: Some(
2042-
This,
2043-
),
2044-
},
2045-
),
2046-
},
2047-
This,
20482022
CallStatement {
20492023
operator: ReferenceExpr {
20502024
kind: Member(
@@ -2054,9 +2028,16 @@ fn this_keyword_can_be_mixed_with_super() {
20542028
),
20552029
base: Some(
20562030
ReferenceExpr {
2057-
kind: Deref,
2031+
kind: Member(
2032+
Super(derefed),
2033+
),
20582034
base: Some(
2059-
This,
2035+
ReferenceExpr {
2036+
kind: Deref,
2037+
base: Some(
2038+
This,
2039+
),
2040+
},
20602041
),
20612042
},
20622043
),
@@ -2071,7 +2052,12 @@ fn this_keyword_can_be_mixed_with_super() {
20712052
},
20722053
),
20732054
base: Some(
2074-
This,
2055+
ReferenceExpr {
2056+
kind: Deref,
2057+
base: Some(
2058+
This,
2059+
),
2060+
},
20752061
),
20762062
},
20772063
right: ReferenceExpr {
@@ -2092,33 +2078,6 @@ fn this_keyword_can_be_mixed_with_super() {
20922078
},
20932079
),
20942080
},
2095-
CallStatement {
2096-
operator: This,
2097-
parameters: None,
2098-
},
2099-
Assignment {
2100-
left: This,
2101-
right: CallStatement {
2102-
operator: ReferenceExpr {
2103-
kind: Member(
2104-
Identifier {
2105-
name: "REF",
2106-
},
2107-
),
2108-
base: None,
2109-
},
2110-
parameters: Some(
2111-
ReferenceExpr {
2112-
kind: Member(
2113-
Identifier {
2114-
name: "fb2",
2115-
},
2116-
),
2117-
base: None,
2118-
},
2119-
),
2120-
},
2121-
},
21222081
]
21232082
"#);
21242083
}

src/parser/tests/parse_errors/parse_error_containers_tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn this_is_a_reserved_keyword() {
307307
error[E007]: Unexpected token: expected KeywordSemicolon but found 'VAR
308308
this'
309309
┌─ <internal>:4:9
310-
310+
311311
4 │ ╭ VAR
312312
5 │ │ this : INT;
313313
│ ╰────────────────^ Unexpected token: expected KeywordSemicolon but found 'VAR
@@ -322,7 +322,7 @@ fn this_is_a_reserved_keyword() {
322322
error[E007]: Unexpected token: expected KeywordSemicolon but found 'END_VAR
323323
METHOD this END_METHOD'
324324
┌─ <internal>:6:9
325-
325+
326326
6 │ ╭ END_VAR
327327
7 │ │ METHOD this END_METHOD
328328
│ ╰──────────────────────────────^ Unexpected token: expected KeywordSemicolon but found 'END_VAR
@@ -345,5 +345,11 @@ fn this_is_a_reserved_keyword() {
345345
346346
3 │ PROGRAM this
347347
│ ^^^^ Case condition used outside of case statement! Did you mean to use ';'?
348+
349+
error[E120]: Invalid use of `THIS`. Usage is only allowed within POU of type `FUNCTION_BLOCK` or type `METHOD`
350+
┌─ <internal>:3:13
351+
352+
3 │ PROGRAM this
353+
│ ^^^^ Invalid use of `THIS`. Usage is only allowed within POU of type `FUNCTION_BLOCK` or type `METHOD`
348354
");
349355
}

src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ impl<'i> TypeAnnotator<'i> {
16501650
let name = match ctx.pou.and_then(|name| self.index.find_pou(name)) {
16511651
Some(PouIndexEntry::FunctionBlock { name, .. }) => name,
16521652
Some(PouIndexEntry::Method { parent_name: name, .. })
1653-
if self.index.find_pou(&name).is_some_and(|it| it.is_function_block()) =>
1653+
if self.index.find_pou(name).is_some_and(|it| it.is_function_block()) =>
16541654
{
16551655
name
16561656
}

src/validation/statement.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,27 @@ pub fn visit_statement<T: AnnotationMap>(
152152
}
153153
}
154154
AstStatement::This => {
155-
dbg!(statement);
156-
// TODO: check if I have an annotation. Only this in fb or method has annoation. If I
157-
// dont have an annotation then something is wrong.
158-
// if context.annotations.get_type(statement, context.index).is_none() {
159-
// if context.annotations.get_type(statement, context.index).is_none() {
160155
if !context.qualifier.is_some_and(|it| {
161-
context.index.find_pou(it).is_some_and(|it| it.is_method() || it.is_function_block())
156+
context
157+
.index
158+
.find_pou(it)
159+
.and_then(|it| {
160+
if it.is_function_block() {
161+
Some(it)
162+
} else {
163+
context.index.find_pou(it.get_parent_pou_name().unwrap_or_default())
164+
}
165+
})
166+
.is_some_and(|it| it.is_function_block())
162167
}) {
163-
//error
164168
validator.push_diagnostic(
165-
Diagnostic::new("Invalid use of `THIS`. Usage is only allowed within POU of type `FUNCTION_BLOCK` or type `METHOD`")
166-
.with_error_code("E120")
167-
.with_location(statement),
169+
Diagnostic::new(
170+
"Invalid use of `THIS`. Usage is only allowed within POU of type `FUNCTION_BLOCK`",
171+
)
172+
.with_error_code("E120")
173+
.with_location(statement),
168174
);
169175
}
170-
// todo!()
171176
}
172177
_ => {}
173178
}

0 commit comments

Comments
 (0)