From 4e139dd295c916170b78018e746b48d5eab7bfbd Mon Sep 17 00:00:00 2001 From: Vyacheslav Egorov Date: Wed, 9 Jun 2021 15:22:48 +0300 Subject: [PATCH] Added behavior of the operator during typechecking Prohibited operator use on a nil type. --- rfcs/syntax-nil-forgiving-operator.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rfcs/syntax-nil-forgiving-operator.md b/rfcs/syntax-nil-forgiving-operator.md index 018e81bb..a4b956b7 100644 --- a/rfcs/syntax-nil-forgiving-operator.md +++ b/rfcs/syntax-nil-forgiving-operator.md @@ -43,7 +43,7 @@ primaryexp ::= prefixexp [`!'] { postfixeexp } When we get the `!` token, we will wrap the expression that we have into a new AstExprNonNilAssertion node. -This operator doesn't have any impact on the run-time behavior of the program, it will only change the type of the expression in the typechecker. +This operator doesn't have any impact on the run-time behavior of the program, it will only affect the type of the expression in the typechecker. --- While parsing an assignment expression starts with a *primaryexp*, it performs a check that it has an l-value based on a fixed set of AstNode types. @@ -54,9 +54,14 @@ p.a! = b ``` --- -If the operator is used on expression of type that is already non-nil, it has no effect and doesn't generate additional warnings. +When operator is used on expression of a union type with a 'nil' option, it removes that option from the set. +If only one option remains, the union type is replaced with the type of a single option. -The reason for this is to simplify movement of existing code where context in each location is slightly different. +If the type is `nil`, typechecker will generate a warning that the operator cannot be applied to the expression. + +For any other type, it has no effect and doesn't generate additional warnings. + +The reason for the last rule is to simplify movement of existing code where context in each location is slightly different. As an example from Roblox, instance path could dynamically change from being know to exist to be missing when script is changed in edit mode.