Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 197 additions & 21 deletions selfhost/sema/src/phases/bodies/narrowing.ens
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ endsInNullCheck(BodyChecker checker, Expression condition, bool conditionHolds,
final class NullComparison {
protected Expression node;
protected bool testHolds;
protected bool whole;

protected constructor(this.node, this.testHolds);
protected constructor(this.node, this.testHolds, this.whole);
}

// The comparison with 'null' in 'condition' that holds only where the place 'path' is null, where
Expand All @@ -392,42 +393,103 @@ nullComparison(BodyChecker checker, Expression condition, bool conditionHolds, N
if (check == null || check.narrowsWhenTrue == conditionHolds || check.path != path) {
return null;
}
return new NullComparison(core, conditionHolds);
return new NullComparison(core, conditionHolds, true);
}

// The branch of an 'if' that a read is in, where that branch runs only where the place it reads is
// null: the comparison with 'null' that proves it, whether that comparison is true there, whether the
// read is in the branch the condition holds in, and whether the branch that runs where the place is
// not null is written.
// The comparison with 'null' that proves the place 'path' is null wherever 'condition' evaluates to
// 'conditionHolds', found in any operand of the '&&' or '||' that proves it, or null where none does.
nullProofIn(BodyChecker checker, Expression condition, bool conditionHolds, NarrowingPath path)
-> NullComparison? {
return nullProofWithin(checker, condition, conditionHolds, path, true);
}

nullProofWithin(BodyChecker checker, Expression condition, bool conditionHolds, NarrowingPath path,
bool whole) -> NullComparison? {
Expression core = syntax.unwrapParentheses(condition);
if (core is PrefixExpressionNode && core.operator().kind() == SyntaxKind.Exclamation) {
return nullProofWithin(checker, core.operand(), !conditionHolds, path, whole);
}
if (core is BinaryExpressionNode && runsRight(core.operator().kind(), conditionHolds)) {
let right = nullProofWithin(checker, core.right(), conditionHolds, path, false);
if (right != null) {
return right;
}
return nullProofWithin(checker, core.left(), conditionHolds, path, false);
}
let found = nullComparison(checker, core, conditionHolds, path);
if (found == null) {
return null;
}
return new NullComparison(found.node, found.testHolds, whole);
}

// Where a read that a test proves null is: the branch of an 'if', the right side of a '&&' or
// '||', a value of a '?:', or the body of a 'while'.
public enum NullSite {
IfBranch,
RightOfAnd,
RightOfOr,
TernaryValue,
LoopBody,
}

// The place a read is in, where it runs only where the place it reads is null: the comparison with
// 'null' that proves it as written and written the other way round, whether that comparison is true
// there, whether the read is in the 'true' side of its test, and whether the other side is written.
public final class NullBranch {
protected NullSite kind;
protected string provenBy;
protected string flippedBy;
protected bool comparisonHolds;
protected bool trueBranch;
protected bool trueSide;
protected bool otherWritten;
protected bool whole;

protected constructor(this.provenBy, this.comparisonHolds, this.trueBranch, this.otherWritten);
protected constructor(this.kind, this.provenBy, this.flippedBy, this.comparisonHolds,
this.trueSide, this.otherWritten, this.whole);

public site() -> NullSite {
return this.kind;
}

public proof() -> string {
return this.provenBy;
}

public flipped() -> string {
return this.flippedBy;
}

public proofHolds() -> bool {
return this.comparisonHolds;
}

// The test that holds exactly where the place is null, as written or the other way round.
public nullTest() -> string {
if (this.comparisonHolds) {
return this.provenBy;
}
return this.flippedBy;
}

public inTrueBranch() -> bool {
return this.trueBranch;
return this.trueSide;
}

public otherBranchWritten() -> bool {
return this.otherWritten;
}

// Whether the test is the whole condition, not one operand of a '&&' or '||'.
public isWhole() -> bool {
return this.whole;
}
}

// The 'if' branch 'value' is read in, where that branch runs only where the place 'value' reads is
// null, or null where no 'if' that encloses the read proves that, the value reads no place, or the
// branch writes the place before the read. A lambda body is read where the lambda runs, not where it
// is written, so no branch outside it proves anything of what it reads.
// The place 'value' is read in, where that place runs only where the place 'value' reads is null,
// or null where no 'if', '?:', '&&', '||' or 'while' that encloses the read proves that, or a
// write comes before the read. A lambda body is read where the lambda runs, not where it is
// written, so no place outside it proves anything of what it reads.
public nullBranchOf(BodyChecker checker, Expression value) -> NullBranch? {
let path = buildNarrowingPath(checker, value);
if (path == null) {
Expand All @@ -438,17 +500,130 @@ public nullBranchOf(BodyChecker checker, Expression value) -> NullBranch? {
if (node is LambdaExpressionNode) {
return null;
}
if (node is IfStatementNode) {
let found = nullBranchIn(checker, node, value, path);
if (found != null) {
return found;
}
let found = nullBranchAt(checker, node, value, path);
if (found != null) {
return found;
}
node = node.parent();
}
return null;
}

// The place 'node' holds 'value' in, where 'node' proves that 'value' is null, or null where it
// does not.
nullBranchAt(BodyChecker checker, SyntaxNode node, Expression value, NarrowingPath path)
-> NullBranch? {
if (node is IfStatementNode) {
return nullBranchIn(checker, node, value, path);
}
if (node is WhileStatementNode) {
return nullLoopIn(checker, node, value, path);
}
if (node is TernaryExpressionNode) {
return nullTernaryIn(checker, node, value, path);
}
if (node is BinaryExpressionNode) {
return nullOperandIn(checker, node, value, path);
}
return null;
}

// The right side of the '&&' or '||' 'node' that holds 'value', where it runs only where 'path' is
// null, or null where 'value' is on the left, no test proves that, or a write comes before the read.
nullOperandIn(BodyChecker checker, BinaryExpressionNode node, Expression value, NarrowingPath path)
-> NullBranch? {
SyntaxKind operator = node.operator().kind();
if (operator != SyntaxKind.AmpersandAmpersand && operator != SyntaxKind.PipePipe) {
return null;
}
if (!within(value.span(), node.right().span())) {
return null;
}
bool runsWhenLeftHolds = operator == SyntaxKind.AmpersandAmpersand;
let comparison = nullProofIn(checker, node.left(), runsWhenLeftHolds, path);
if (comparison == null) {
return null;
}
SyntaxNode right = node.right();
long at = value.span().start;
if (writesNameBefore(checker, node.left(), at, path)
|| writesNameBefore(checker, right, at, path)
|| writesNameInLoopAround(checker, value, right, path)) {
return null;
}
NullSite site = NullSite.RightOfOr;
if (runsWhenLeftHolds) {
site = NullSite.RightOfAnd;
}
return new NullBranch(site, wording.oneLineSpelling(comparison.node),
flippedTest(comparison.node), comparison.testHolds, true, false, comparison.whole);
}

// The value of the '?:' 'node' that holds 'value', where it runs only where 'path' is null, or null
// where 'value' is in neither value, no test proves that, or a write comes before the read.
nullTernaryIn(BodyChecker checker, TernaryExpressionNode node, Expression value, NarrowingPath path)
-> NullBranch? {
bool inThen = within(value.span(), node.thenValue().span());
bool inElse = within(value.span(), node.elseValue().span());
if (inThen == inElse) {
return null;
}
let comparison = nullProofIn(checker, node.condition(), inThen, path);
if (comparison == null) {
return null;
}
SyntaxNode branch = node.elseValue();
if (inThen) {
branch = node.thenValue();
}
long at = value.span().start;
if (writesNameBefore(checker, node.condition(), at, path)
|| writesNameBefore(checker, branch, at, path)
|| writesNameInLoopAround(checker, value, branch, path)) {
return null;
}
return new NullBranch(NullSite.TernaryValue, wording.oneLineSpelling(comparison.node),
flippedTest(comparison.node), comparison.testHolds, inThen, true, comparison.whole);
}

// The body of the 'while' 'node' that holds 'value', where it runs only where 'path' is null, or
// null where no test of the condition proves that, or a write comes before the read in the loop.
nullLoopIn(BodyChecker checker, WhileStatementNode node, Expression value, NarrowingPath path)
-> NullBranch? {
if (!within(value.span(), node.body().span())) {
return null;
}
let comparison = nullProofIn(checker, node.condition(), true, path);
if (comparison == null) {
return null;
}
long at = value.span().start;
if (writesNameBefore(checker, node.condition(), at, path)
|| writesNameBefore(checker, node.body(), at, path)
|| writesNameInLoopAround(checker, value, node.body(), path)) {
return null;
}
return new NullBranch(NullSite.LoopBody, wording.oneLineSpelling(comparison.node),
flippedTest(comparison.node), comparison.testHolds, true, true, comparison.whole);
}

// The comparison 'comparison' with its test written the other way round, as 'b != null'.
flippedTest(Expression comparison) -> string {
if (!(comparison is BinaryExpressionNode)) {
panic("Internal: a null comparison is not a binary expression");
}
string operator = "==";
if (comparison.operator().kind() == SyntaxKind.EqualsEquals) {
operator = "!=";
}
Expression left = syntax.unwrapParentheses(comparison.left());
Expression place = left;
if (wording.isNullLiteral(left)) {
place = syntax.unwrapParentheses(comparison.right());
}
return "{wording.oneLineSpelling(place)} {operator} null";
}

// The branch of 'statement' that holds 'value', where it runs only where 'path' is null, or null where
// the read is in neither branch, no test proves that, or the branch writes the place before the read.
nullBranchIn(BodyChecker checker, IfStatementNode statement, Expression value, NarrowingPath path)
Expand All @@ -459,7 +634,7 @@ nullBranchIn(BodyChecker checker, IfStatementNode statement, Expression value, N
if (inThen == inElse) {
return null;
}
let comparison = nullComparison(checker, statement.condition(), inThen, path);
let comparison = nullProofIn(checker, statement.condition(), inThen, path);
if (comparison == null) {
return null;
}
Expand All @@ -481,8 +656,9 @@ nullBranchIn(BodyChecker checker, IfStatementNode statement, Expression value, N
|| writesNameInLoopAround(checker, value, branch, path)) {
return null;
}
return new NullBranch(wording.oneLineSpelling(comparison.node), comparison.testHolds, inThen,
otherWritten);
return new NullBranch(NullSite.IfBranch, wording.oneLineSpelling(comparison.node),
flippedTest(comparison.node), comparison.testHolds, inThen, otherWritten,
comparison.whole);
}

// Whether a part of 'node' that comes before the offset 'at' writes a local or a parameter named for
Expand Down
56 changes: 48 additions & 8 deletions selfhost/sema/src/phases/bodies/nullguidance.ens
Original file line number Diff line number Diff line change
Expand Up @@ -750,26 +750,66 @@ nullBranchRefusal(BodyChecker checker, string opening, NullJoin join, Expression
return withGuidance(checker, why, nullBranchAdvice(branch, spelled, verb));
}

// The advice for the use 'verb' names of the value 'spelled' that the branch 'branch' proves is null:
// why the branch proves it, and where the same use is not refused.
// The advice for the use 'verb' names of the value 'spelled' that the place 'branch' proves is
// null: the test that proves it, and where the same use is not refused.
nullBranchAdvice(NullBranch branch, string spelled, string verb) -> string {
string name = "'{spelled}'";
string ask = typenames.capitalizedAscii(verb);
string proof = "'{branch.proof()}'";
string why = "This branch runs only when {proof}.";
string holds = "'{branch.proof()}'";
if (!branch.proofHolds()) {
why = "This branch runs only when {proof} is false.";
holds = "'{branch.proof()}' is false";
}
switch (branch.site()) {
IfBranch -> { return ifBranchAdvice(branch, holds, name, ask); },
RightOfAnd -> { return rightOfAdvice("'&&'", branch, holds, name); },
RightOfOr -> { return rightOfAdvice("'||'", branch, holds, name); },
TernaryValue -> { return ternaryValueAdvice(branch, holds, name, ask); },
LoopBody -> { return loopBodyAdvice(branch, holds, name, ask); },
}
}

// The advice for a test that is one operand of the '&&' or '||' in the condition of an 'if' or '?:'
// that the place 'name' is null in: the test written the other way round, where 'name' is not null.
operandAdvice(NullBranch branch, string holds, string name, string subject, string condition)
-> string {
return "{subject} runs only when {holds}. Write '{branch.flipped()}' in place of '{branch.proof()}' "
+ "in the {condition} condition, so {name} is not null where it runs.";
}

ternaryValueAdvice(NullBranch branch, string holds, string name, string ask) -> string {
if (!branch.isWhole()) {
return operandAdvice(branch, holds, name, "This value", "'?:'");
}
return "This value runs only when {holds}. {ask} it in the other value of the '?:', "
+ "where {name} is not null.";
}

ifBranchAdvice(NullBranch branch, string holds, string name, string ask) -> string {
if (!branch.isWhole()) {
return operandAdvice(branch, holds, name, "This branch", "'if'");
}
string runs = "This branch runs only when {holds}.";
if (!branch.inTrueBranch()) {
return "{why} {ask} it in the 'if' branch, where {name} is not null.";
return "{runs} {ask} it in the 'if' branch, where {name} is not null.";
}
if (branch.otherBranchWritten()) {
return "{why} {ask} it in the 'else' branch, where {name} is not null.";
return "{runs} {ask} it in the 'else' branch, where {name} is not null.";
}
return "{why} {ask} it in an 'else' branch, or after the 'if' once this branch has returned, "
return "{runs} {ask} it in an 'else' branch, or after the 'if' once this branch has returned, "
+ "where {name} is not null.";
}

rightOfAdvice(string operator, NullBranch branch, string holds, string name) -> string {
return "The right side of {operator} runs only when {holds}. Write '{branch.flipped()}' in "
+ "place of '{branch.proof()}', so {name} is not null where the right side runs.";
}

loopBodyAdvice(NullBranch branch, string holds, string name, string ask) -> string {
string leave = "if ({branch.nullTest()}) \{ continue; \}";
return "The body of this 'while' runs only when {holds}. {ask} it after an 'if' that leaves "
+ "the iteration when {name} is null, such as '{leave}', where {name} is not null.";
}

// Both operands of a binary operator and their fixes. Two locals that would share a name get
// 'left' and 'right' in front of it.
classifyOperands(BodyChecker checker, Expression leftNode, Expression rightNode, Type left, Type right,
Expand Down
Loading
Loading