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
99 changes: 97 additions & 2 deletions selfhost/sema/src/phases/bodies/memberaccess.ens
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SuperExpressionNode from @ens.frontend.syntax.generated.nodes;
import SafeMemberExpressionNode from @ens.frontend.syntax.generated.nodes;
import SubscriptExpressionNode from @ens.frontend.syntax.generated.nodes;
import SafeSubscriptExpressionNode from @ens.frontend.syntax.generated.nodes;
import TernaryExpressionNode from @ens.frontend.syntax.generated.nodes;
import SwitchExpressionNode from @ens.frontend.syntax.generated.nodes;
import Span from @ens.frontend.support.source;
import ModuleContext from context;
import FunctionEntry from context;
Expand All @@ -17,6 +19,7 @@ import model.convert;
import phases.members;
import phases.bodies.operators;
import phases.bodies.nullguidance;
import phases.bodies.mutation;
import phases.bodies.wording;
import phases.bodies.voidcalls;
import phases.bodies.narrowing;
Expand Down Expand Up @@ -634,6 +637,92 @@ public checkConstructorAccess(BodyChecker checker, Type ownerType, Method chosen
}
}

// The refusal of a 'mutating' method called on the struct that a '?.' read of a name gives, as in
// 'holder?.plain.bump()'. The call changes a copy, so the fix writes the read without '?.'. Null where
// the receiver has another form, where a branch proves the read null, or where the read is too long
// to name.
copiedCallRefusal(BodyChecker checker, Expression object, Type objectType, Type core, string name,
MemberPosition position) -> string? {
if (position != MemberPosition.Covered || wording.optionalLevels(objectType) != 1) {
return null;
}
if (!isMutatingCall(checker, core, name)) {
return null;
}
if (!(object is SafeMemberExpressionNode)) {
return null;
}
let guard = object.object();
if (!(guard is IdentifierExpressionNode)) {
return null;
}
let spelledRead = wording.shortSpelling(object);
if (spelledRead == null || narrowing.nullBranchOf(checker, object) != null) {
return null;
}
string guardName = guard.name().text();
string member = object.member().text();
return "Cannot call '{name}' on '{spelledRead}': a '?.' gives a copy, so changes to it would be "
+ "lost. Check '{guardName}' for null first ('if ({guardName} != null)') and write "
+ "'{guardName}.{member}'.";
}

// Whether a call of 'name' on 'core' is a call of a 'mutating' method of a struct value.
isMutatingCall(BodyChecker checker, Type core, string name) -> bool {
switch (lookupMember(checker, core, name)) {
is MethodMember found -> {
return namesMutatingStructMethod(core, found.candidates);
},
default -> {
return false;
},
}
}

// The read of an optional value as its message names it: '?:' for a join, 'switch' for a switch,
// and '?.' otherwise.
readForm(Expression object) -> string {
switch (syntax.unwrapParentheses(object)) {
is TernaryExpressionNode ignored -> {
return "'?:'";
},
is SwitchExpressionNode ignored -> {
return "switch";
},
default -> {
return "'?.'";
},
}
}

// Why a change to the struct value read through 'form' is lost, or null where a '??' fallback can
// hold it. 'form' is the read as written: '?.', '?:' or a switch. A class or an array changes through
// the reference it carries, which a fallback supplies as well as any other value, so it keeps it.
fallbackRefusal(BodyChecker checker, Type core, string name, MemberPosition position, string form)
-> string? {
if (!mutation.changesWithItsValue(core)) {
return null;
}
if (position == MemberPosition.Write) {
return "A {form} gives a copy, so a write to it would be lost.";
}
if (position != MemberPosition.Covered) {
return null;
}
switch (lookupMember(checker, core, name)) {
is MethodMember found -> {
if (!namesMutatingStructMethod(core, found.candidates)) {
return null;
}
return "'{name}' is 'mutating', which marks a method that changes the struct it is called "
+ "on. A {form} gives a copy, so changes to it would be lost.";
},
default -> {
return null;
},
}
}

// A covered access reports no method here. A call is covered because it takes the method
// this resolves to from the side table this sets. 'writtenMember' names the member a write sets
// on the field this reads, as 'left' in 'this.pair.left = ...'.
Expand Down Expand Up @@ -696,9 +785,15 @@ public analyzeMemberValue(BodyChecker checker, MemberExpressionNode node,
node.span());
return checker.store().errorType();
}
Type unwrappedCore = memberReceiverCore(checker, unwrapped, name);
let copied = copiedCallRefusal(checker, object, objectType, unwrappedCore, name, position);
if (copied != null) {
checker.reporter().report(copied, node.span());
return checker.store().errorType();
}
checker.reporter().report(nullguidance.maybeNullRefusal(checker, "Cannot {use} a member of "
+ checker.names().quoted(objectType), nullguidance.NullJoin.Because, object, objectType, use),
node.span());
+ checker.names().quoted(objectType), nullguidance.NullJoin.Because, object, objectType, use,
fallbackRefusal(checker, unwrappedCore, name, position, readForm(object))), node.span());
return checker.store().errorType();
}
Type core = memberReceiverCore(checker, objectType, name);
Expand Down
2 changes: 1 addition & 1 deletion selfhost/sema/src/phases/bodies/mutation.ens
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public placeRoot(BodyChecker checker, Expression holder) -> PlaceRoot? {

// Whether a change to a field of a value of 'type' changes the value itself: a struct, or a type
// parameter, which a struct can be passed as.
changesWithItsValue(Type type) -> bool {
public changesWithItsValue(Type type) -> bool {
return types.isStructLike(type) || type is TypeParamType;
}

Expand Down
Loading
Loading