summaryrefslogtreecommitdiff
path: root/vala/valamemberaccess.vala
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2021-02-13 19:34:12 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2021-02-13 19:34:12 +0100
commite199f5621175c47a1b3c22a5c8c6337412df6fb9 (patch)
tree8498e585cb4a03db9d32be70f11054b88b244321 /vala/valamemberaccess.vala
parent869a81a7a3f06f363ef52ef2e535b710681913d2 (diff)
downloadvala-e199f5621175c47a1b3c22a5c8c6337412df6fb9.tar.gz
vala: Include type-checks in preconditions of methods for type narrowing
See https://gitlab.gnome.org/GNOME/vala/issues/894
Diffstat (limited to 'vala/valamemberaccess.vala')
-rw-r--r--vala/valamemberaccess.vala20
1 files changed, 20 insertions, 0 deletions
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 06cf64ce4..735f1b79e 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -1130,6 +1130,7 @@ public class Vala.MemberAccess : Expression {
bool is_negation = false;
unowned CodeNode? parent = parent_node;
unowned IfStatement? if_statement = null;
+ var scope_type_checks = new ArrayList<unowned TypeCheck> ();
while (parent != null && !(parent is Method)) {
if (parent is TypeCheck) {
parent = null;
@@ -1140,6 +1141,14 @@ public class Vala.MemberAccess : Expression {
is_negation = if_statement.false_statement == parent;
break;
}
+ if (parent.parent_node is Method) {
+ foreach (Expression expr in ((Method) parent.parent_node).get_preconditions ()) {
+ if (expr is TypeCheck) {
+ scope_type_checks.add ((TypeCheck) expr);
+ }
+ }
+ break;
+ }
parent = parent.parent_node;
}
@@ -1160,5 +1169,16 @@ public class Vala.MemberAccess : Expression {
}
}
}
+ if (value_type.context_symbol == null) {
+ foreach (TypeCheck type_check in scope_type_checks) {
+ unowned TypeSymbol? narrowed_symnol = type_check.type_reference.type_symbol;
+ if (variable == type_check.expression.symbol_reference) {
+ if (narrowed_symnol != value_type.type_symbol) {
+ value_type.context_symbol = narrowed_symnol;
+ }
+ value_type.nullable = false;
+ }
+ }
+ }
}
}