@@ -978,18 +978,18 @@ are generic.
978
978
This will cause an error:
979
979
980
980
```compile_fail
981
- #![feature(simd )]
981
+ #![feature(repr_simd )]
982
982
983
- #[simd]
983
+ #[repr( simd) ]
984
984
struct Bad<T>(T, T, T);
985
985
```
986
986
987
987
This will not:
988
988
989
989
```
990
- #![feature(simd )]
990
+ #![feature(repr_simd )]
991
991
992
- #[simd]
992
+ #[repr( simd) ]
993
993
struct Good(u32, u32, u32);
994
994
```
995
995
"## ,
@@ -1026,18 +1026,18 @@ will trigger this error.
1026
1026
This will cause an error:
1027
1027
1028
1028
```compile_fail
1029
- #![feature(simd )]
1029
+ #![feature(repr_simd )]
1030
1030
1031
- #[simd]
1031
+ #[repr( simd) ]
1032
1032
struct Bad(u16, u32, u32);
1033
1033
```
1034
1034
1035
1035
This will not:
1036
1036
1037
1037
```
1038
- #![feature(simd )]
1038
+ #![feature(repr_simd )]
1039
1039
1040
- #[simd]
1040
+ #[repr( simd) ]
1041
1041
struct Good(u32, u32, u32);
1042
1042
```
1043
1043
"## ,
@@ -1049,18 +1049,18 @@ must be machine types so SIMD operations can be applied to them.
1049
1049
This will cause an error:
1050
1050
1051
1051
```compile_fail
1052
- #![feature(simd )]
1052
+ #![feature(repr_simd )]
1053
1053
1054
- #[simd]
1054
+ #[repr( simd) ]
1055
1055
struct Bad(String);
1056
1056
```
1057
1057
1058
1058
This will not:
1059
1059
1060
1060
```
1061
- #![feature(simd )]
1061
+ #![feature(repr_simd )]
1062
1062
1063
- #[simd]
1063
+ #[repr( simd) ]
1064
1064
struct Good(u32, u32, u32);
1065
1065
```
1066
1066
"## ,
@@ -2387,39 +2387,135 @@ impl Copy for &'static Bar { } // error
2387
2387
"## ,
2388
2388
2389
2389
E0207 : r##"
2390
- You declared an unused type parameter when implementing a trait on an object.
2391
- Erroneous code example:
2390
+ Any type parameter or lifetime parameter of an `impl` must meet at least one of
2391
+ the following criteria:
2392
+
2393
+ - it appears in the self type of the impl
2394
+ - for a trait impl, it appears in the trait reference
2395
+ - it is bound as an associated type
2396
+
2397
+ ### Error example 1
2398
+
2399
+ Suppose we have a struct `Foo` and we would like to define some methods for it.
2400
+ The following definition leads to a compiler error:
2392
2401
2393
2402
```compile_fail
2394
- trait MyTrait {
2395
- fn get(&self) -> usize;
2403
+ struct Foo;
2404
+
2405
+ impl<T: Default> Foo {
2406
+ // error: the type parameter `T` is not constrained by the impl trait, self
2407
+ // type, or predicates [E0207]
2408
+ fn get(&self) -> T {
2409
+ <T as Default>::default()
2410
+ }
2396
2411
}
2412
+ ```
2413
+
2414
+ The problem is that the parameter `T` does not appear in the self type (`Foo`)
2415
+ of the impl. In this case, we can fix the error by moving the type parameter
2416
+ from the `impl` to the method `get`:
2397
2417
2418
+
2419
+ ```
2398
2420
struct Foo;
2399
2421
2400
- impl<T> MyTrait for Foo {
2401
- fn get(&self) -> usize {
2402
- 0
2422
+ // Move the type parameter from the impl to the method
2423
+ impl Foo {
2424
+ fn get<T: Default>(&self) -> T {
2425
+ <T as Default>::default()
2403
2426
}
2404
2427
}
2405
2428
```
2406
2429
2407
- Please check your object definition and remove unused type
2408
- parameter(s). Example:
2430
+ ### Error example 2
2431
+
2432
+ As another example, suppose we have a `Maker` trait and want to establish a
2433
+ type `FooMaker` that makes `Foo`s:
2434
+
2435
+ ```compile_fail
2436
+ trait Maker {
2437
+ type Item;
2438
+ fn make(&mut self) -> Self::Item;
2439
+ }
2440
+
2441
+ struct Foo<T> {
2442
+ foo: T
2443
+ }
2444
+
2445
+ struct FooMaker;
2409
2446
2447
+ impl<T: Default> Maker for FooMaker {
2448
+ // error: the type parameter `T` is not constrained by the impl trait, self
2449
+ // type, or predicates [E0207]
2450
+ type Item = Foo<T>;
2451
+
2452
+ fn make(&mut self) -> Foo<T> {
2453
+ Foo { foo: <T as Default>::default() }
2454
+ }
2455
+ }
2410
2456
```
2411
- trait MyTrait {
2412
- fn get(&self) -> usize;
2457
+
2458
+ This fails to compile because `T` does not appear in the trait or in the
2459
+ implementing type.
2460
+
2461
+ One way to work around this is to introduce a phantom type parameter into
2462
+ `FooMaker`, like so:
2463
+
2464
+ ```
2465
+ use std::marker::PhantomData;
2466
+
2467
+ trait Maker {
2468
+ type Item;
2469
+ fn make(&mut self) -> Self::Item;
2413
2470
}
2414
2471
2415
- struct Foo;
2472
+ struct Foo<T> {
2473
+ foo: T
2474
+ }
2416
2475
2417
- impl MyTrait for Foo {
2418
- fn get(&self) -> usize {
2419
- 0
2476
+ // Add a type parameter to `FooMaker`
2477
+ struct FooMaker<T> {
2478
+ phantom: PhantomData<T>,
2479
+ }
2480
+
2481
+ impl<T: Default> Maker for FooMaker<T> {
2482
+ type Item = Foo<T>;
2483
+
2484
+ fn make(&mut self) -> Foo<T> {
2485
+ Foo {
2486
+ foo: <T as Default>::default(),
2487
+ }
2420
2488
}
2421
2489
}
2422
2490
```
2491
+
2492
+ Another way is to do away with the associated type in `Maker` and use an input
2493
+ type parameter instead:
2494
+
2495
+ ```
2496
+ // Use a type parameter instead of an associated type here
2497
+ trait Maker<Item> {
2498
+ fn make(&mut self) -> Item;
2499
+ }
2500
+
2501
+ struct Foo<T> {
2502
+ foo: T
2503
+ }
2504
+
2505
+ struct FooMaker;
2506
+
2507
+ impl<T: Default> Maker<Foo<T>> for FooMaker {
2508
+ fn make(&mut self) -> Foo<T> {
2509
+ Foo { foo: <T as Default>::default() }
2510
+ }
2511
+ }
2512
+ ```
2513
+
2514
+ ### Additional information
2515
+
2516
+ For more information, please see [RFC 447].
2517
+
2518
+ [RFC 447]: https://github.com/rust-lang/rfcs/blob/master/text/0447-no-unused-impl-parameters.md
2423
2519
"## ,
2424
2520
2425
2521
E0210 : r##"
0 commit comments