Skip to main content

Module narrowing

Module narrowing 

Source
Expand description

Which is tests an expression proves. #1654 (runtime-semantics track S5, Decision A): the one structural rule the resolver, the checker and the emitter all read, so their views of an is binding’s scope cannot drift.

An e is P test with bindings brings those bindings into scope wherever the test is known to have matched. Given a Boolean expression and whether it evaluated to true or false, matched_is_tests returns the is tests that must then have matched:

expressionwhen truewhen false
e is Pthe test—
a && ba when true, b when true— (either may have failed)
a || b— (either may have held)a when false, b when false
!ee when falsee when true
a implies b— (!a || b)a when true, b when false
(e)ee

The consumers apply it at these scopes:

  • an if’s then-branch: its condition when true;
  • an if’s else-branch: its condition when false (if !(o is Some(v)) { … } else { v }, type-system §2.3.6);
  • the right operand of && and implies: the left operand when true, since the right is evaluated only then.

The right operand of || (the left operand when false) is not scoped in this slice: nothing lowers it with bindings yet, and a scope the checker accepts must be one the emitter lowers.

Functions§

matched_is_tests
The is tests (ExprKind::Is nodes) that expr proves matched when it evaluates to when_true, in source order.