-
Notifications
You must be signed in to change notification settings - Fork 137
/
check_reference_expression.go
189 lines (158 loc) Β· 5.6 KB
/
check_reference_expression.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sema
import (
"github.com/onflow/cadence/ast"
)
// VisitReferenceExpression checks a reference expression
func (checker *Checker) VisitReferenceExpression(referenceExpression *ast.ReferenceExpression) Type {
resultType := checker.expectedType
if resultType == nil {
checker.report(
&TypeAnnotationRequiredError{
Cause: "cannot infer type from reference expression:",
Pos: referenceExpression.Expression.StartPosition(),
},
)
return InvalidType
}
// Check the result type and ensure it is a reference type
var referenceType *ReferenceType
var expectedLeftType, returnType Type
if !resultType.IsInvalidType() {
// Reference expressions may reference a value which has an optional type.
// For example, the result of indexing into a dictionary is an optional:
//
// let ints: {Int: String} = {0: "zero"}
// let ref: &T? = &ints[0] as &T? // read as (&T)?
//
// In this case the reference expression's type is an optional type.
// Unwrap it (recursively) to get the actual reference type
expectedLeftType, returnType, referenceType =
checker.expectedTypeForReferencedExpr(resultType, referenceExpression)
}
// Type-check the referenced expression
referencedExpression := referenceExpression.Expression
beforeErrors := len(checker.errors)
referencedType, actualType := checker.visitExpression(referencedExpression, referenceExpression, expectedLeftType)
// check that the type of the referenced value is not itself a reference
var requireNoReferenceNesting func(actualType Type)
requireNoReferenceNesting = func(actualType Type) {
switch nestedReference := actualType.(type) {
case *ReferenceType:
checker.report(&NestedReferenceError{
Type: nestedReference,
Range: checker.expressionRange(referenceExpression),
})
case *OptionalType:
requireNoReferenceNesting(nestedReference.Type)
}
}
requireNoReferenceNesting(actualType)
hasErrors := len(checker.errors) > beforeErrors
if !hasErrors {
checker.checkOptionalityMatch(
expectedLeftType,
actualType,
referencedExpression,
referenceExpression,
)
}
if referenceType == nil {
return InvalidType
}
checker.checkUnusedExpressionResourceLoss(referencedType, referencedExpression)
checker.Elaboration.SetReferenceExpressionBorrowType(referenceExpression, returnType)
return returnType
}
func (checker *Checker) expectedTypeForReferencedExpr(
expectedType Type,
hasPosition ast.HasPosition,
) (expectedLeftType, returnType Type, referenceType *ReferenceType) {
// Reference expressions may reference a value which has an optional type.
// For example, the result of indexing into a dictionary is an optional:
//
// let ints: {Int: String} = {0: "zero"}
// let ref: &T? = &ints[0] as &T? // read as (&T)?
//
// In this case the reference expression's type is an optional type.
// Unwrap it to get the actual reference type
switch expectedType := expectedType.(type) {
case *OptionalType:
expectedLeftType, returnType, referenceType =
checker.expectedTypeForReferencedExpr(expectedType.Type, hasPosition)
// Re-wrap with an optional
expectedLeftType = &OptionalType{Type: expectedLeftType}
returnType = &OptionalType{Type: returnType}
case *ReferenceType:
referencedType := expectedType.Type
if referencedOptionalType, referenceToOptional := referencedType.(*OptionalType); referenceToOptional {
checker.report(
&ReferenceToAnOptionalError{
ReferencedOptionalType: referencedOptionalType,
Range: ast.NewRangeFromPositioned(checker.memoryGauge, hasPosition),
},
)
}
return expectedType.Type, expectedType, expectedType
default:
checker.report(
&NonReferenceTypeReferenceError{
ActualType: expectedType,
Range: ast.NewRangeFromPositioned(checker.memoryGauge, hasPosition),
},
)
return InvalidType, InvalidType, nil
}
return
}
func (checker *Checker) checkOptionalityMatch(
expectedType, actualType Type,
referencedExpression ast.Expression,
referenceExpression ast.Expression,
) {
// Do not report an error if the `expectedType` is unknown
if expectedType == nil || expectedType.IsInvalidType() {
return
}
// If the reference type was an optional type,
// we proposed an optional type to the referenced expression.
//
// Check that it actually has an optional type
// If the reference type was a non-optional type,
// check that the referenced expression does not have an optional type
expectedOptional, expectedIsOptional := expectedType.(*OptionalType)
actualOptional, actualIsOptional := actualType.(*OptionalType)
if expectedIsOptional && actualIsOptional {
checker.checkOptionalityMatch(
expectedOptional.Type,
actualOptional.Type,
referencedExpression,
referenceExpression,
)
return
}
if expectedIsOptional != actualIsOptional {
checker.report(&TypeMismatchError{
ExpectedType: expectedType,
ActualType: actualType,
Expression: referencedExpression,
Range: checker.expressionRange(referenceExpression),
})
}
}