@@ -286,28 +286,6 @@ In this example, `Foo::Alpha { x: 5 }` is allowed when it is in the same crate a
286
286
because ` x ` is not restricted within this scope, so the field can be freely mutated. Because of
287
287
this, the previous concern about upholding variants is not applicable.
288
288
289
- ## ` mod ` as a restriction scope
290
-
291
- ` pub ` can be restricted in a few ways. The most common is ` pub(crate) ` , which restricts visibility
292
- to the current crate. However, there are other ways to restrict visibility. You can also use
293
- ` pub(super) ` to restrict visibility to the parent module, and ` pub(in path) ` to restrict visibility
294
- to ` path ` , as long as that path is an ancestor of the location it is used. There is one additional
295
- case you have likely never encountered: ` pub(self) ` . The reason you have likely never seen this
296
- before is that it is redundant: ` pub(self) ` is identical to private, which is the default
297
- visibility.
298
-
299
- While ` pub(self) ` should never be needed in most code, ` impl(self) ` and ` mut(self) ` are quite
300
- different. As no restrictions apply by default (anything visible can be implemented/mutated), is
301
- more than likely that ` impl(self) ` and ` mut(self) ` will be quite common. During previous discussions
302
- about syntax, the unclear meaning of ` impl(self) ` and ` mut(self) ` was brought up. Syntax should have
303
- a clear meaning, and ` impl(self) ` and ` mut(self) ` are not clear. While they should be allowed for
304
- consistency with ` pub(self) ` , an alternative was needed with the same behavior. ` impl(mod) ` and
305
- ` mut(mod) ` should have a sufficiently clear meaning: implementing the trait and mutating the field
306
- are restricted to the current module. ` impl(in mod) ` and ` mut(in mod) ` are accepted as well. The
307
- behavior of ` impl(mod) ` is identical to ` impl(self) ` ; likewise for ` mut(mod) ` and ` mut(self) ` .
308
-
309
- For consistency with the new restriction syntax, ` pub(mod) ` and ` pub(in mod) ` are also allowed.
310
-
311
289
# Reference-level explanation
312
290
313
291
## Syntax
@@ -334,10 +312,8 @@ TupleField :
334
312
+ mut
335
313
+ | mut ( crate )
336
314
+ | mut ( self )
337
- + | mut ( mod )
338
315
+ | mut ( super )
339
316
+ | mut ( in SimplePath )
340
- + | mut ( in mod )
341
317
```
342
318
343
319
Trait definitions need a similar change to the [ syntax for ` trait ` s] [ trait syntax ] to accommodate
@@ -359,27 +335,12 @@ Trait :
359
335
+ impl
360
336
+ | impl ( crate )
361
337
+ | impl ( self )
362
- + | impl ( mod )
363
338
+ | impl ( super )
364
339
+ | impl ( in SimplePath )
365
- + | impl ( in mod )
366
340
```
367
341
368
342
Essentially, ` mut ` and ` impl ` have the same syntax as ` pub ` , just with a different keyword.
369
343
370
- Finally, a minor change is needed to the syntax for visibility to permit ` pub(mod) ` .
371
-
372
- ``` diff
373
- Visibility :
374
- pub
375
- | pub ( crate )
376
- | pub ( self )
377
- + | pub ( mod )
378
- | pub ( super )
379
- | pub ( in SimplePath )
380
- + | pub ( in mod )
381
- ```
382
-
383
344
## Behavior
384
345
385
346
The current behavior of ` pub ` is that ` pub ` makes something visible within the declared scope. If no
@@ -391,8 +352,6 @@ variants, `enum` fields, and `trait` items. `impl` and `mut` will have a consist
391
352
omitted entirely, the scope is inherited from ` pub ` . This is both what is most convenient and is
392
353
what is required for backwards compatibility with existing code.
393
354
394
- ` pub(mod) ` is equivalent to ` pub(self) ` ; likewise for ` impl(mod) ` and ` mut(mod) ` .
395
-
396
355
When an ` ImplRestriction ` is present, implementations of the associated trait are only permitted
397
356
within the designated path. Any implementation of the trait outside this scope is a compile error.
398
357
When a ` MutRestriction ` is present, mutable uses of the associated field are only permitted within
@@ -470,3 +429,4 @@ Trait aliases cannot be implemented. As such, there is no concern about compatib
470
429
- The default could be changed in a future edition, such as to make ` pub field: Type ` be only
471
430
mutable within the module rather than mutable everywhere. This seems unlikely, as it would be an
472
431
incredibly disruptive change, and the benefits would have to be significant.
432
+ - Syntax such as ` impl(mod) ` could be added for clarity as an alternative to ` impl(self) ` .
0 commit comments