diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2021-02-13 19:34:12 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2021-02-13 19:34:12 +0100 |
commit | e199f5621175c47a1b3c22a5c8c6337412df6fb9 (patch) | |
tree | 8498e585cb4a03db9d32be70f11054b88b244321 /vala/valamemberaccess.vala | |
parent | 869a81a7a3f06f363ef52ef2e535b710681913d2 (diff) | |
download | vala-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.vala | 20 |
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; + } + } + } } } |