Commit Graph

66 Commits

Author SHA1 Message Date
bors
e24252a12c Auto merge of #68970 - matthewjasper:min-spec, r=nikomatsakis
Implement a feature for a sound specialization subset

This implements a new feature (`min_specialization`) that restricts specialization to a subset that is reasonable for the standard library to use.

The plan is to then:

* Update `libcore` and `liballoc` to compile with `min_specialization`.
* Add a lint to forbid use of `feature(specialization)` (and other unsound, type system extending features) in the standard library.
* Fix the soundness issues around `specialization`.
* Remove `min_specialization`

The rest of this is an overview from a comment in this PR

## Basic approach

To enforce this requirement on specializations we take the following approach:
1. Match up the substs for `impl2` so that the implemented trait and self-type match those for `impl1`.
2. Check for any direct use of `'static` in the substs of `impl2`.
3. Check that all of the generic parameters of `impl1` occur at most once in the *unconstrained* substs for `impl2`. A parameter is constrained if its value is completely determined by an associated type projection predicate.
4. Check that all predicates on `impl1` also exist on `impl2` (after matching substs).

## Example

Suppose we have the following always applicable impl:

```rust
impl<T> SpecExtend<T> for std::vec::IntoIter<T> { /* specialized impl */ }
impl<T, I: Iterator<Item=T>> SpecExtend<T> for I { /* default impl */ }
```

We get that the subst for `impl2` are `[T, std::vec::IntoIter<T>]`. `T` is constrained to be `<I as Iterator>::Item`, so we check only `std::vec::IntoIter<T>` for repeated parameters, which it doesn't have. The predicates of `impl1` are only `T: Sized`, which is also a predicate of impl2`. So this specialization is sound.

## Extensions

Unfortunately not all specializations in the standard library are allowed by this. So there are two extensions to these rules that allow specializing on some traits.

### rustc_specialization_trait

If a trait is always applicable, then it's sound to specialize on it. We check trait is always applicable in the same way as impls, except that step 4 is now "all predicates on `impl1` are always applicable". We require that `specialization` or `min_specialization` is enabled to implement these traits.

### rustc_specialization_marker

There are also some specialization on traits with no methods, including the `FusedIterator` trait which is advertised as allowing optimizations. We allow marking marker traits with an unstable attribute that means we ignore them in point 3 of the checks above. This is unsound but we allow it in the short term because it can't cause use after frees with purely safe code in the same way as specializing on traits methods can.

r? @nikomatsakis
cc #31844 #67194
2020-03-16 20:49:26 +00:00
Vadim Petrochenkov
65bf4831d2 ast/hir: MacroDef::legacy -> MacroDef::macro_rules 2020-03-16 00:29:03 +03:00
Mazdak Farrokhzad
d1e943f263
Rollup merge of #69589 - petrochenkov:maccall, r=Centril
ast: `Mac`/`Macro` -> `MacCall`

It's now obvious that these refer to macro calls rather than to macro definitions.

It's also a single name instead of two different names in different places.

`rustc_expand` usually calls macro calls in a wide sense (including attributes and derives) "macro invocations", but structures and variants renamed in this PR are only relevant to fn-like macros, so it's simpler and clearer to just call them calls.

cc https://github.com/rust-lang/rust/pull/63586#discussion_r314232513
r? @eddyb
2020-03-15 15:40:05 +01:00
Matthew Jasper
a62dd0e3ba Add min_specialization feature
Currently the only difference between it and `specialization` is that
it only allows specializing functions.
2020-03-15 12:44:25 +00:00
Vadim Petrochenkov
e809e0214e ast: Mac/Macro -> MacCall 2020-03-12 22:26:52 +03:00
Mazdak Farrokhzad
4f7fc5ad67
Rollup merge of #69722 - estebank:negative-impl-span-ast, r=Centril
Tweak output for invalid negative impl AST errors

Use more accurate spans for negative `impl` errors.

r? @Centril
2020-03-12 16:32:17 +01:00
Esteban Küber
7ee1b47092 review comments 2020-03-11 09:17:55 -07:00
Esteban Küber
29be741c9c review comments 2020-03-10 17:59:32 -07:00
Mazdak Farrokhzad
61150353bf
Rollup merge of #69514 - GuillaumeGomez:remove-spotlight, r=kinnison
Remove spotlight

I had a few comments saying that this feature was at best misunderstood or not even used so I decided to organize a poll about on [twitter](https://twitter.com/imperioworld_/status/1232769353503956994). After 87 votes, the result is very clear: it's not useful. Considering the amount of code we have just to run it, I think it's definitely worth it to remove it.

r? @kinnison

cc @ollie27
2020-03-10 06:47:47 +01:00
Esteban Küber
f483032e97 review comment 2020-03-06 11:58:52 -08:00
Esteban Küber
005fc6eacc Suggest removal of auto trait super traits and type params 2020-03-06 11:58:41 -08:00
Esteban Küber
6fba412499 Further tweak spans in ast validation errors 2020-03-06 10:55:21 -08:00
Esteban Küber
713a291441 review comments 2020-03-05 15:39:35 -08:00
Esteban Küber
91525fd078 Tweak output for invalid negative impl AST errors 2020-03-04 16:15:23 -08:00
Vadim Petrochenkov
857e34c7a3 ast: Unmerge structures for associated items and foreign items 2020-03-01 13:47:48 +03:00
Vadim Petrochenkov
e08c279eac Rename syntax to rustc_ast in source code 2020-02-29 21:59:09 +03:00
Vadim Petrochenkov
6054a30370 Make it build again 2020-02-29 20:47:10 +03:00
bors
6d69caba11 Auto merge of #68434 - varkor:astconv-mismatch-error, r=nikomatsakis
Move generic arg/param validation to `create_substs_for_generic_args` to resolve various const generics issues

This changes some diagnostics, but I think they're around as helpful as the previous ones, and occur infrequently regardless.

Fixes https://github.com/rust-lang/rust/issues/68257.
Fixes https://github.com/rust-lang/rust/issues/68398.

r? @eddyb
2020-02-27 18:38:19 +00:00
Guillaume Gomez
13c6d5819a Remove spotlight usage 2020-02-27 14:51:22 +01:00
Mazdak Farrokhzad
62930d3151 parse/ast: move Defaultness into variants. 2020-02-24 00:59:38 +01:00
Mazdak Farrokhzad
fa2a792491 add Span to ast::Defaultness::Default. 2020-02-24 00:59:38 +01:00
varkor
76fb26b8c2 Add invalid argument spans to GenericArgCountMismatch 2020-02-22 11:34:29 +00:00
varkor
039045c49b Move generic arg / param validation to create_substs_for_generic_args 2020-02-22 00:28:18 +00:00
Mazdak Farrokhzad
9f3dfd29a2 parse: allow type Foo: Ord syntactically. 2020-02-22 00:19:27 +01:00
Mazdak Farrokhzad
b864d23f34
Rollup merge of #69194 - Centril:assoc-extern-fuse, r=petrochenkov
parse: fuse associated and extern items up to defaultness

Language changes:

- The grammar of extern `type` aliases is unified with associated ones, and becomes:
  ```rust
  TypeItem = "type" ident generics {":" bounds}? where_clause {"=" type}? ";" ;
  ```

  Semantic restrictions (`ast_validation`) are added to forbid any parameters in `generics`, any bounds in `bounds`, and any predicates in `where_clause`, as well as the presence of a type expression (`= u8`).

  (Work still remains to fuse this with free `type` aliases, but this can be done later.)

- The grammar of constants and static items (free, associated, and extern) now permits the absence of an expression, and becomes:

  ```rust
  GlobalItem = {"const" {ident | "_"} | "static" "mut"? ident} {"=" expr}? ";" ;
  ```

  - A semantic restriction is added to enforce the presence of the expression (the body).
  - A semantic restriction is added to reject `const _` in associated contexts.

Together, these changes allow us to fuse the grammar of associated items and extern items up to `default`ness which is the main goal of the PR.

-----------------------

We are now very close to fully fusing the entirely of item parsing and their ASTs. To progress further, we must make a decision: should we parse e.g. `default use foo::bar;` and whatnot? Accepting that is likely easiest from a parsing perspective, as it does not require using look-ahead, but it is perhaps not too onerous to only accept it for `fn`s (and all their various qualifiers), `const`s, `static`s, and `type`s.

r? @petrochenkov
2020-02-18 22:16:26 +01:00
Yuki Okushi
eb12ed889d Rename FunctionRetTy to FnRetTy 2020-02-17 11:24:29 +09:00
Mazdak Farrokhzad
d6238bd8d4 reject assoc statics & extern consts during parsing 2020-02-15 22:21:00 +01:00
Mazdak Farrokhzad
35884fe168 parse extern consts 2020-02-15 20:57:12 +01:00
Mazdak Farrokhzad
f8d2264463 parse associated statics. 2020-02-15 20:57:12 +01:00
Mazdak Farrokhzad
1c2906ead3 ast/parser: fuse static & const grammars in all contexts. 2020-02-15 20:57:12 +01:00
Mazdak Farrokhzad
f3e9763543 ast: make = <expr>; optional in free statics/consts. 2020-02-15 20:57:12 +01:00
Mazdak Farrokhzad
95dc9b9a73 ast: normalize ForeignItemKind::Ty & AssocItemKind::TyAlias. 2020-02-15 18:00:01 +01:00
Mazdak Farrokhzad
2fd15442f2 ast: move Generics into AssocItemKinds 2020-02-15 18:00:01 +01:00
Mazdak Farrokhzad
3341c94006 ast_validation: tweak diagnostic output 2020-02-13 10:40:17 +01:00
Mazdak Farrokhzad
a833be2162 parser: fuse free fn parsing together. 2020-02-13 10:39:24 +01:00
Mazdak Farrokhzad
c30f068dc8 IsAsync -> enum Async { Yes { span: Span, .. }, No }
use new span for better diagnostics.
2020-02-13 10:39:24 +01:00
Mazdak Farrokhzad
e839b2ec84 Constness -> enum Const { Yes(Span), No }
Same idea for `Unsafety` & use new span for better diagnostics.
2020-02-13 10:39:23 +01:00
Matthias Prechtl
f35a7c38da Make issue references consistent 2020-02-09 20:43:49 +01:00
Mazdak Farrokhzad
9a4eac3944 ast_validation: fix visiting bug. 2020-02-05 12:27:45 +01:00
Mazdak Farrokhzad
b2c6eeb713 parser: merge fn grammars wrt. bodies & headers
also refactor `FnKind` and `visit_assoc_item` visitors
2020-02-05 01:27:09 +01:00
bors
126ad2b813 Auto merge of #68708 - Mark-Simulacrum:stage0-step, r=pietroalbini
Step stage0 to bootstrap from 1.42

This also includes a commit which fixes the rustfmt downloading logic to redownload when the rustfmt channel changes, and bumps rustfmt to a more recent version.
2020-02-04 14:16:18 +00:00
Mazdak Farrokhzad
5951cd3dda
Rollup merge of #68764 - Centril:self-semantic, r=petrochenkov
parser: syntactically allow `self` in all `fn` contexts

Part of https://github.com/rust-lang/rust/pull/68728.

`self` parameters are now *syntactically* allowed as the first parameter irrespective of item context (and in function pointers). Instead, semantic validation (`ast_validation`) is used.

r? @petrochenkov
2020-02-02 14:15:52 +01:00
Mazdak Farrokhzad
71a6f58229 parser: address review comments re. self. 2020-02-02 13:32:37 +01:00
Mazdak Farrokhzad
8674efdb7c parser: move restrictions re. self to ast_validation. 2020-02-02 10:33:55 +01:00
Mazdak Farrokhzad
e233331a51 syntax::print -> new crate rustc_ast_pretty 2020-02-01 18:59:49 +01:00
Mazdak Farrokhzad
097d5e1c5e 1. move node_id to syntax
2. invert rustc_session & syntax deps
3. drop rustc_session dep in rustc_hir
2020-02-01 18:58:08 +01:00
Mazdak Farrokhzad
93a8283614 Move builtin attribute logic to new rustc_attr crate.
For now, this is all the crate contains, but more
attribute logic & types will be moved there over time.
2020-02-01 18:54:56 +01:00
Mazdak Farrokhzad
50f0e2e9e6 {syntax -> rustc_ast_passes}::node_count 2020-02-01 18:54:55 +01:00
Mark Rousskov
31dcdc9e13 Drop cfg(bootstrap) code 2020-01-31 12:31:09 -05:00
Mazdak Farrokhzad
3484e2fab4
Rollup merge of #68140 - ecstatic-morse:const-trait-bound-opt-out, r=oli-obk
Implement `?const` opt-out for trait bounds

For now, such bounds are treated exactly the same as unprefixed ones in all contexts. [RFC 2632](https://github.com/rust-lang/rfcs/pull/2632) does not specify whether such bounds are forbidden outside of `const` contexts, so they are allowed at the moment.

Prior to this PR, the constness of a trait bound/impl was stored in `TraitRef`. Now, the constness of an `impl` is stored in `ast::ItemKind::Impl` and the constness of a bound in `ast::TraitBoundModifer`. Additionally, constness of trait bounds is now stored in an additional field of `ty::Predicate::Trait`, and the combination of the constness of the item along with any `TraitBoundModifier` determines the constness of the bound in accordance with the RFC. Encoding the constness of impls at the `ty` level is left for a later PR.

After a discussion in \#wg-grammar on Discord, it was decided that the grammar should not encode the mutual exclusivity of trait bound modifiers. The grammar for trait bound modifiers remains `[?const] [?]`. To encode this, I add a dummy variant to `ast::TraitBoundModifier` that is used when the syntax `?const ?` appears. This variant causes an error in AST validation and disappears during HIR lowering.

cc #67794

r? @oli-obk
2020-01-21 19:42:20 +01:00