|
| 1 | +;; Validation of `outer` aliases that cross the boundary of a `component` type: |
| 2 | +;; like a `component` definition, a `component` type may not alias a type that |
| 3 | +;; transitively refers to a resource type, whereas `instance` type boundaries |
| 4 | +;; are transparent. |
| 5 | + |
| 6 | +;; a `component` type may alias the resource-free types of its enclosing |
| 7 | +;; component... |
| 8 | + |
| 9 | +(component definition $C |
| 10 | + (core type $CT (func (param i32) (result i32))) |
| 11 | + (type $T (tuple u32 string)) |
| 12 | + (type $F (func (param "t" $T) (result $T))) |
| 13 | + (type $I (instance (export "f" (func (type $F))))) |
| 14 | + (type $U (component (import "i" (instance (type $I))))) |
| 15 | + (type (component |
| 16 | + (alias outer $C $CT (core type $ct)) |
| 17 | + (alias outer $C $T (type $t)) |
| 18 | + (alias outer $C $F (type $f)) |
| 19 | + (alias outer $C $I (type $i)) |
| 20 | + (alias outer $C $U (type $u)) |
| 21 | + (import "f" (func (type $f))) |
| 22 | + (import "i" (instance (type $i))) |
| 23 | + (import "c" (component (type $u))) |
| 24 | + (export "g" (func (param "t" $t))) |
| 25 | + )) |
| 26 | +) |
| 27 | + |
| 28 | +;; ...including instance and component types that use resource types, as long |
| 29 | +;; as each resource type is imported or exported by the aliased type itself |
| 30 | + |
| 31 | +(component definition $C |
| 32 | + (type $I (instance |
| 33 | + (export "r" (type $r (sub resource))) |
| 34 | + (export "f" (func (param "self" (borrow $r)))) |
| 35 | + )) |
| 36 | + (type $U (component |
| 37 | + (import "r" (type $r (sub resource))) |
| 38 | + (export "s" (type $s (sub resource))) |
| 39 | + (export "f" (func (param "r" (own $r)) (result (own $s)))) |
| 40 | + )) |
| 41 | + (type (component |
| 42 | + (alias outer $C $I (type $i)) |
| 43 | + (alias outer $C $U (type $u)) |
| 44 | + (import "i" (instance (type $i))) |
| 45 | + (export "c" (component (type $u))) |
| 46 | + )) |
| 47 | +) |
| 48 | + |
| 49 | +;; a `component` type may also alias the resource types that it imports or |
| 50 | +;; exports itself, either directly (ct=0) or from nested `instance` types |
| 51 | +;; (crossing only `instance` type boundaries) |
| 52 | + |
| 53 | +(component definition |
| 54 | + (type $CT (component |
| 55 | + (import "r" (type $R (sub resource))) |
| 56 | + (alias outer $CT $R (type $r0)) |
| 57 | + (import "f" (func (param "r" (borrow $r0)))) |
| 58 | + (type $IT (instance |
| 59 | + (alias outer $CT $R (type $r1)) |
| 60 | + (export "f" (func (param "r" (borrow $r1)))) |
| 61 | + (type (instance |
| 62 | + (alias outer $CT $R (type $r2)) |
| 63 | + (export "g" (func (result (own $r2)))) |
| 64 | + )) |
| 65 | + )) |
| 66 | + (import "i" (instance (type $IT))) |
| 67 | + (export "j" (instance (type $IT))) |
| 68 | + (export "s" (type $S (sub resource))) |
| 69 | + (export "k" (instance |
| 70 | + (export "h" (func (param "r" (borrow $R)) (result (own $S)))) |
| 71 | + )) |
| 72 | + )) |
| 73 | +) |
| 74 | + |
| 75 | +;; (just as `instance` types nested in a component may alias the component's |
| 76 | +;; resource types) |
| 77 | + |
| 78 | +(component definition $C |
| 79 | + (type $R (resource (rep i32))) |
| 80 | + (type (instance |
| 81 | + (alias outer $C $R (type $r)) |
| 82 | + (type (instance |
| 83 | + (alias outer $C $R (type $r)) |
| 84 | + (export "f" (func (param "r" (borrow $r)))) |
| 85 | + )) |
| 86 | + )) |
| 87 | +) |
| 88 | + |
| 89 | +;; however, a `component` type may not alias a resource type of its enclosing |
| 90 | +;; component, whether defined, imported or exported... |
| 91 | + |
| 92 | +(assert_invalid |
| 93 | + (component $C |
| 94 | + (type $R (resource (rep i32))) |
| 95 | + (type (component (alias outer $C $R (type $r)))) |
| 96 | + ) |
| 97 | + "transitively refers to resources") |
| 98 | + |
| 99 | +(assert_invalid |
| 100 | + (component $C |
| 101 | + (import "r" (type $R (sub resource))) |
| 102 | + (type (component (alias outer $C $R (type $r)))) |
| 103 | + ) |
| 104 | + "transitively refers to resources") |
| 105 | + |
| 106 | +(assert_invalid |
| 107 | + (component $C |
| 108 | + (type $R (resource (rep i32))) |
| 109 | + (export $R' "r" (type $R)) |
| 110 | + (type (component (alias outer $C $R' (type $r)))) |
| 111 | + ) |
| 112 | + "transitively refers to resources") |
| 113 | + |
| 114 | +(assert_invalid |
| 115 | + (component |
| 116 | + (type (resource (rep i32))) |
| 117 | + (type (component (alias outer 1 0 (type)))) |
| 118 | + ) |
| 119 | + "transitively refers to resources") |
| 120 | + |
| 121 | +;; ...nor any type that transitively refers to one |
| 122 | + |
| 123 | +(assert_invalid |
| 124 | + (component $C |
| 125 | + (type $R (resource (rep i32))) |
| 126 | + (type $B (borrow $R)) |
| 127 | + (type (component (alias outer $C $B (type $b)))) |
| 128 | + ) |
| 129 | + "transitively refers to resources") |
| 130 | + |
| 131 | +(assert_invalid |
| 132 | + (component $C |
| 133 | + (type $R (resource (rep i32))) |
| 134 | + (type $L (list (own $R))) |
| 135 | + (type (component (alias outer $C $L (type $l)))) |
| 136 | + ) |
| 137 | + "transitively refers to resources") |
| 138 | + |
| 139 | +(assert_invalid |
| 140 | + (component $C |
| 141 | + (type $R (resource (rep i32))) |
| 142 | + (type $F (func (param "r" (borrow $R)))) |
| 143 | + (type (component (alias outer $C $F (type $f)))) |
| 144 | + ) |
| 145 | + "transitively refers to resources") |
| 146 | + |
| 147 | +(assert_invalid |
| 148 | + (component $C |
| 149 | + (type $R (resource (rep i32))) |
| 150 | + (type $I (instance (export "r" (type (eq $R))))) |
| 151 | + (type (component (alias outer $C $I (type $i)))) |
| 152 | + ) |
| 153 | + "transitively refers to resources") |
| 154 | + |
| 155 | +;; the implicit `outer` aliases injected by text-format name resolution are |
| 156 | +;; subject to the same rule |
| 157 | + |
| 158 | +(assert_invalid |
| 159 | + (component |
| 160 | + (type $R (resource (rep i32))) |
| 161 | + (type (component (import "r" (type (eq $R))))) |
| 162 | + ) |
| 163 | + "transitively refers to resources") |
| 164 | + |
| 165 | +(assert_invalid |
| 166 | + (component |
| 167 | + (type $R (resource (rep i32))) |
| 168 | + (type (component (export "r" (type (eq $R))))) |
| 169 | + ) |
| 170 | + "transitively refers to resources") |
| 171 | + |
| 172 | +(assert_invalid |
| 173 | + (component |
| 174 | + (import "r" (type $R (sub resource))) |
| 175 | + (type $I (instance (export "r" (type (eq $R))))) |
| 176 | + (type (component (import "i" (instance (type $I))))) |
| 177 | + ) |
| 178 | + "transitively refers to resources") |
| 179 | + |
| 180 | +;; the rule applies wherever a `component` type is written, including inline |
| 181 | +;; as the type of an import, so that an `outer` alias of an imported component |
| 182 | +;; can always be replaced by a copy of the import |
| 183 | + |
| 184 | +(assert_invalid |
| 185 | + (component $C |
| 186 | + (import "r" (type $R (sub resource))) |
| 187 | + (import "c" (component |
| 188 | + (alias outer $C $R (type $r)) |
| 189 | + (export "r" (type (eq $r))) |
| 190 | + )) |
| 191 | + ) |
| 192 | + "transitively refers to resources") |
| 193 | + |
| 194 | +;; an alias is rejected if any of the boundaries it crosses is a `component` |
| 195 | +;; type boundary: an `instance` type in a `component` type... |
| 196 | + |
| 197 | +(assert_invalid |
| 198 | + (component $C |
| 199 | + (type $R (resource (rep i32))) |
| 200 | + (type (component |
| 201 | + (type (instance (alias outer $C $R (type $r)))) |
| 202 | + )) |
| 203 | + ) |
| 204 | + "transitively refers to resources") |
| 205 | + |
| 206 | +(assert_invalid |
| 207 | + (component |
| 208 | + (import "r" (type $R (sub resource))) |
| 209 | + (type (component |
| 210 | + (import "i" (instance (export "r" (type (eq $R))))) |
| 211 | + )) |
| 212 | + ) |
| 213 | + "transitively refers to resources") |
| 214 | + |
| 215 | +;; ...a `component` type in an `instance` type... |
| 216 | + |
| 217 | +(assert_invalid |
| 218 | + (component $C |
| 219 | + (type $R (resource (rep i32))) |
| 220 | + (type (instance |
| 221 | + (type (component (alias outer $C $R (type $r)))) |
| 222 | + )) |
| 223 | + ) |
| 224 | + "transitively refers to resources") |
| 225 | + |
| 226 | +(assert_invalid |
| 227 | + (component |
| 228 | + (import "r" (type $R (sub resource))) |
| 229 | + (import "i" (instance |
| 230 | + (export "c" (component (export "r" (type (eq $R))))) |
| 231 | + )) |
| 232 | + ) |
| 233 | + "transitively refers to resources") |
| 234 | + |
| 235 | +;; ...and a `component` type in a `component` type |
| 236 | + |
| 237 | +(assert_invalid |
| 238 | + (component $C |
| 239 | + (type $R (resource (rep i32))) |
| 240 | + (type (component |
| 241 | + (type (component (alias outer $C $R (type $r)))) |
| 242 | + )) |
| 243 | + ) |
| 244 | + "transitively refers to resources") |
0 commit comments