Skip to content

Commit bd4d580

Browse files
authored
Replace addr with ref in design docs (#6141)
Updates the documentation under `docs/design/` to use `ref` instead of `addr` after their removal in #5434. Care was taken to manually clean up edge cases and, in a couple cases, surrounding text (see 3d72c49). After this change, there are no matches for `addr(?!ess)` in `docs/design/`. Closes #6032
1 parent b1c0854 commit bd4d580

File tree

13 files changed

+149
-161
lines changed

13 files changed

+149
-161
lines changed

docs/design/README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,16 +1767,16 @@ class Point {
17671767
return Math.Sqrt(dx * dx + dy * dy);
17681768
}
17691769
// Mutating method declaration
1770-
fn Offset[addr self: Self*](dx: i32, dy: i32);
1770+
fn Offset[ref self: Self](dx: i32, dy: i32);
17711771
17721772
var x: i32;
17731773
var y: i32;
17741774
}
17751775
17761776
// Out-of-line definition of method declared inline
1777-
fn Point.Offset[addr self: Self*](dx: i32, dy: i32) {
1778-
self->x += dx;
1779-
self->y += dy;
1777+
fn Point.Offset[ref self: Self](dx: i32, dy: i32) {
1778+
self.x += dx;
1779+
self.y += dy;
17801780
}
17811781
17821782
var origin: Point = {.x = 0, .y = 0};
@@ -1796,10 +1796,9 @@ two methods `Distance` and `Offset`:
17961796
- `Distance` computes and returns the distance to another point, without
17971797
modifying the `Point`. This is signified using `[self: Self]` in the method
17981798
declaration.
1799-
- `origin.Offset(`...`)` does modify the value of `origin`. This is signified
1800-
using `[addr self: Self*]` in the method declaration. Since calling this
1801-
method requires taking the [non-`const`](#const) address of `origin`, it may
1802-
only be called on [reference expressions](#expression-categories).
1799+
- `origin.Offset(`...`)` _does_ modify the value of `origin`. This is
1800+
signified using `[ref self: Self]` in the method declaration. It may only be
1801+
called on [reference expressions](#expression-categories).
18031802
- Methods may be declared lexically inline like `Distance`, or lexically out
18041803
of line like `Offset`.
18051804

@@ -1955,7 +1954,7 @@ names resolvable by the compiler, and don't act like forward declarations.
19551954

19561955
A destructor for a class is custom code executed when the lifetime of a value of
19571956
that type ends. They are defined with `fn destroy` followed by either
1958-
`[self: Self]` or `[addr self: Self*]` (as is done with [methods](#methods)) and
1957+
`[self: Self]` or `[ref self: Self]` (as is done with [methods](#methods)) and
19591958
the block of code in the class definition, as in:
19601959

19611960
```carbon
@@ -1969,7 +1968,7 @@ or:
19691968
```carbon
19701969
class MyClass {
19711970
// Can modify `self` in the body.
1972-
fn destroy[addr self: Self*]() { ... }
1971+
fn destroy[ref self: Self]() { ... }
19731972
}
19741973
```
19751974

@@ -2004,8 +2003,8 @@ For every type `MyClass`, there is the type `const MyClass` such that:
20042003
has type `const T`.
20052004
- While all of the member names in `MyClass` are also member names in
20062005
`const MyClass`, the effective API of a `const MyClass` reference expression
2007-
is a subset of `MyClass`, because only `addr` methods accepting a
2008-
`const Self*` will be valid.
2006+
is a subset of `MyClass`, because only `ref` methods accepting a
2007+
`const Self` will be valid.
20092008

20102009
Note that `const` binds more tightly than postfix-`*` for forming a pointer
20112010
type, so `const MyClass*` is equal to `(const MyClass)*`.
@@ -2996,8 +2995,8 @@ associated constant to represent the type of elements stored in the stack.
29962995
```
29972996
interface StackInterface {
29982997
let ElementType:! Movable;
2999-
fn Push[addr self: Self*](value: ElementType);
3000-
fn Pop[addr self: Self*]() -> ElementType;
2998+
fn Push[ref self: Self](value: ElementType);
2999+
fn Pop[ref self: Self]() -> ElementType;
30013000
fn IsEmpty[self: Self]() -> bool;
30023001
}
30033002
```
@@ -3008,14 +3007,14 @@ for the `ElementType` member of the interface using a `where` clause:
30083007
```carbon
30093008
class IntStack {
30103009
extend impl as StackInterface where .ElementType = i32 {
3011-
fn Push[addr self: Self*](value: i32);
3010+
fn Push[ref self: Self](value: i32);
30123011
// ...
30133012
}
30143013
}
30153014
30163015
class FruitStack {
30173016
extend impl as StackInterface where .ElementType = Fruit {
3018-
fn Push[addr self: Self*](value: Fruit);
3017+
fn Push[ref self: Self](value: Fruit);
30193018
// ...
30203019
}
30213020
}
@@ -3043,8 +3042,8 @@ values of any type `T`:
30433042

30443043
```carbon
30453044
class Stack(T:! type) {
3046-
fn Push[addr self: Self*](value: T);
3047-
fn Pop[addr self: Self*]() -> T;
3045+
fn Push[ref self: Self](value: T);
3046+
fn Pop[ref self: Self]() -> T;
30483047
30493048
var storage: Array(T);
30503049
}

docs/design/assignment.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ standard library.
7373
## Syntax
7474

7575
The operands of these operators can be any [expression](expressions/README.md).
76-
However, the first operand must be modifiable because it is passed to an
77-
`[addr self: Self*]` parameter, which disallows most expression forms other
78-
than:
76+
However, the first operand must be modifiable because it is passed to a
77+
`[ref self: Self]` parameter, which disallows most expression forms other than:
7978

8079
- The name of a `var` binding.
8180
- A dereference of a pointer.
@@ -175,7 +174,7 @@ provided for built-in types as necessary to give the semantics described above.
175174
```
176175
// Simple `=`.
177176
interface AssignWith(U:! type) {
178-
fn Op[addr self: Self*](other: U);
177+
fn Op[ref self: Self](other: U);
179178
}
180179
constraint Assign { extend AssignWith(Self); }
181180
```
@@ -189,48 +188,48 @@ Given `var x: T` and `y: U`:
189188
```
190189
// Compound `+=`.
191190
interface AddAssignWith(U:! type) {
192-
fn Op[addr self: Self*](other: U);
191+
fn Op[ref self: Self](other: U);
193192
}
194193
constraint AddAssign { extend AddAssignWith(Self); }
195194
```
196195

197196
```
198197
// Compound `-=`.
199198
interface SubAssignWith(U:! type) {
200-
fn Op[addr self: Self*](other: U);
199+
fn Op[ref self: Self](other: U);
201200
}
202201
constraint SubAssign { extend SubAssignWith(Self); }
203202
```
204203

205204
```
206205
// Compound `*=`.
207206
interface MulAssignWith(U:! type) {
208-
fn Op[addr self: Self*](other: U);
207+
fn Op[ref self: Self](other: U);
209208
}
210209
constraint MulAssign { extend MulAssignWith(Self); }
211210
```
212211

213212
```
214213
// Compound `/=`.
215214
interface DivAssignWith(U:! type) {
216-
fn Op[addr self: Self*](other: U);
215+
fn Op[ref self: Self](other: U);
217216
}
218217
constraint DivAssign { extend DivAssignWith(Self); }
219218
```
220219

221220
```
222221
// Compound `%=`.
223222
interface ModAssignWith(U:! type) {
224-
fn Op[addr self: Self*](other: U);
223+
fn Op[ref self: Self](other: U);
225224
}
226225
constraint ModAssign { extend ModAssignWith(Self); }
227226
```
228227

229228
```
230229
// Increment `++`.
231-
interface Inc { fn Op[addr self: Self*](); }
230+
interface Inc { fn Op[ref self: Self](); }
232231
// Decrement `++`.
233-
interface Dec { fn Op[addr self: Self*](); }
232+
interface Dec { fn Op[ref self: Self](); }
234233
```
235234

236235
Given `var x: T` and `y: U`:
@@ -248,39 +247,39 @@ Given `var x: T` and `y: U`:
248247
```
249248
// Compound `&=`.
250249
interface BitAndAssignWith(U:! type) {
251-
fn Op[addr self: Self*](other: U);
250+
fn Op[ref self: Self](other: U);
252251
}
253252
constraint BitAndAssign { extend BitAndAssignWith(Self); }
254253
```
255254

256255
```
257256
// Compound `|=`.
258257
interface BitOrAssignWith(U:! type) {
259-
fn Op[addr self: Self*](other: U);
258+
fn Op[ref self: Self](other: U);
260259
}
261260
constraint BitOrAssign { extend BitOrAssignWith(Self); }
262261
```
263262

264263
```
265264
// Compound `^=`.
266265
interface BitXorAssignWith(U:! type) {
267-
fn Op[addr self: Self*](other: U);
266+
fn Op[ref self: Self](other: U);
268267
}
269268
constraint BitXorAssign { extend BitXorAssignWith(Self); }
270269
```
271270

272271
```
273272
// Compound `<<=`.
274273
interface LeftShiftAssignWith(U:! type) {
275-
fn Op[addr self: Self*](other: U);
274+
fn Op[ref self: Self](other: U);
276275
}
277276
constraint LeftShiftAssign { extend LeftShiftAssignWith(Self); }
278277
```
279278

280279
```
281280
// Compound `>>=`.
282281
interface RightShiftAssignWith(U:! type) {
283-
fn Op[addr self: Self*](other: U);
282+
fn Op[ref self: Self](other: U);
284283
}
285284
constraint RightShiftAssign { extend RightShiftAssignWith(Self); }
286285
```
@@ -310,7 +309,7 @@ This defaulting is accomplished by a parameterized implementation of
310309
```
311310
impl forall [U:! type, T:! OpWith(U) where .Self impls AssignWith(.Self.Result)]
312311
T as OpAssignWith(U) {
313-
fn Op[addr self: Self*](other: U) {
312+
fn Op[ref self: Self](other: U) {
314313
// Here, `$` is the operator described by `OpWith`.
315314
*self = *self $ other;
316315
}

docs/design/classes.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -903,14 +903,14 @@ class Circle {
903903
fn Diameter[self: Self]() -> f32 {
904904
return self.radius * 2;
905905
}
906-
fn Expand[addr self: Self*](distance: f32);
906+
fn Expand[ref self: Self](distance: f32);
907907
908908
var center: Point;
909909
var radius: f32;
910910
}
911911
912-
fn Circle.Expand[addr self: Self*](distance: f32) {
913-
self->radius += distance;
912+
fn Circle.Expand[ref self: Self](distance: f32) {
913+
self.radius += distance;
914914
}
915915
916916
var c: Circle = {.center = Point.Origin(), .radius = 1.5 };
@@ -925,12 +925,11 @@ Assert(Math.Abs(c.Diameter() - 4.0) < 0.001);
925925
the `Circle` instance. This is signified using `[self: Self]` in the method
926926
declaration.
927927
- `c.Expand(`...`)` does modify the value of `c`. This is signified using
928-
`[addr self: Self*]` in the method declaration.
928+
`[ref self: Self]` in the method declaration.
929929

930-
The pattern '`addr self:` _type_' means "first take the address of the argument,
931-
which must be an
932-
[l-value](<https://en.wikipedia.org/wiki/Value_(computer_science)#lrvalue>), and
933-
then match pattern '`self:` _type_' against it".
930+
The pattern '`ref self:` _type_' means "the argument must be a
931+
[reference expression](/docs/design/values.md#reference-expressions), and must
932+
match the pattern '`self:` _type_'".
934933

935934
If the method declaration also includes
936935
[deduced compile-time parameters](/docs/design/generics/overview.md#deduced-parameters),
@@ -1585,7 +1584,7 @@ or:
15851584
```carbon
15861585
class MyClass {
15871586
// Can modify `self` in the body.
1588-
fn destroy[addr self: Self*]() { ... }
1587+
fn destroy[ref self: Self]() { ... }
15891588
}
15901589
```
15911590

@@ -1608,9 +1607,9 @@ Destructors may be declared in class scope and then defined out-of-line:
16081607

16091608
```carbon
16101609
class MyClass {
1611-
fn destroy[addr self: Self*]();
1610+
fn destroy[ref self: Self]();
16121611
}
1613-
fn MyClass.destroy[addr self: Self*]() { ... }
1612+
fn MyClass.destroy[ref self: Self]() { ... }
16141613
```
16151614

16161615
It is illegal to delete an instance of a derived class through a pointer to one
@@ -1622,12 +1621,12 @@ must be `override`:
16221621

16231622
```carbon
16241623
base class MyBaseClass {
1625-
virtual fn destroy[addr self: Self*]() { ... }
1624+
virtual fn destroy[ref self: Self]() { ... }
16261625
}
16271626
16281627
class MyDerivedClass {
16291628
extend base: MyBaseClass;
1630-
override fn destroy[addr self: Self*]() { ... }
1629+
override fn destroy[ref self: Self]() { ... }
16311630
}
16321631
```
16331632

@@ -1677,8 +1676,8 @@ call the `UnsafeDelete` method instead. Note that you may not call
16771676
```
16781677
interface Allocator {
16791678
// ...
1680-
fn Delete[T:! Deletable, addr self: Self*](p: T*);
1681-
fn UnsafeDelete[T:! Destructible, addr self: Self*](p: T*);
1679+
fn Delete[T:! Deletable, ref self: Self](p: T*);
1680+
fn UnsafeDelete[T:! Destructible, ref self: Self](p: T*);
16821681
}
16831682
```
16841683

@@ -1835,10 +1834,10 @@ base class MyBaseClass {
18351834
18361835
class MyDerivedClass {
18371836
extend base: MyBaseClass;
1838-
fn UsesProtected[addr self: Self*]() {
1837+
fn UsesProtected[ref self: Self]() {
18391838
// Can access protected members in derived class
18401839
var x: i32 = HelperClassFunction(3);
1841-
self->data = self->HelperMethod(x);
1840+
self.data = self.HelperMethod(x);
18421841
}
18431842
}
18441843
```

docs/design/expressions/member_access.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ For example:
7676
namespace Widgets;
7777
7878
interface Widgets.Widget {
79-
fn Grow[addr self: Self*](factor: f64);
79+
fn Grow[ref self: Self](factor: f64);
8080
}
8181
8282
class Widgets.Cog {
@@ -765,19 +765,15 @@ If instance binding is performed:
765765
766766
- For a method, the result is a _bound method_, which is a value `F` such that
767767
a function call `F(args)` behaves the same as a call to `M(args)` with the
768-
`self` parameter initialized by a corresponding recipient argument:
769-
770-
- If the method declares its `self` parameter with `addr`, the recipient
771-
argument is `&x`.
772-
- Otherwise, the recipient argument is `x`.
768+
`self` parameter initialized by `x`.
773769
774770
```carbon
775771
class Blob {
776-
fn Mutate[addr self: Self*](n: i32);
772+
fn Mutate[ref self: Self](n: i32);
777773
}
778774
fn F(p: Blob*) {
779775
// ✅ OK, forms bound method `((*p).M)` and calls it.
780-
// This calls `Blob.Mutate` with `self` initialized by `&(*p)`
776+
// This calls `Blob.Mutate` with `self` initialized by `*p`
781777
// and `n` initialized by `5`.
782778
(*p).Mutate(5);
783779

docs/design/generics/appendix-witness.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ witness table.
241241

242242
```
243243
interface Iterator {
244-
fn Advance[addr self: Self*]();
244+
fn Advance[ref self: Self]();
245245
}
246246
247247
interface Container {
248248
let IteratorType:! Iterator;
249-
fn Begin[addr self: Self*]() -> IteratorType;
249+
fn Begin[ref self: Self]() -> IteratorType;
250250
}
251251
```
252252

0 commit comments

Comments
 (0)