Skip to content

Commit bf1afe1

Browse files
Treat str as containing [u8] for auto trait purposes
1 parent 2b3f260 commit bf1afe1

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ pub(super) fn instantiate_constituent_tys_for_auto_trait<'tcx>(
1818
| ty::Float(_)
1919
| ty::FnDef(..)
2020
| ty::FnPtr(_)
21-
| ty::Str
2221
| ty::Error(_)
2322
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
2423
| ty::Never
2524
| ty::Char => Ok(vec![]),
2625

26+
// Treat this like `struct str([u8]);`
27+
ty::Str => Ok(vec![infcx.tcx.mk_slice(infcx.tcx.types.u8)]),
28+
2729
ty::Dynamic(..)
2830
| ty::Param(..)
2931
| ty::Foreign(..)

compiler/rustc_trait_selection/src/traits/select/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2250,12 +2250,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
22502250
| ty::Float(_)
22512251
| ty::FnDef(..)
22522252
| ty::FnPtr(_)
2253-
| ty::Str
22542253
| ty::Error(_)
22552254
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
22562255
| ty::Never
22572256
| ty::Char => ty::Binder::dummy(Vec::new()),
22582257

2258+
// Treat this like `struct str([u8]);`
2259+
ty::Str => ty::Binder::dummy(vec![self.tcx().mk_slice(self.tcx().types.u8)]),
2260+
22592261
ty::Placeholder(..)
22602262
| ty::Dynamic(..)
22612263
| ty::Param(..)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(negative_impls)]
2+
#![feature(auto_traits)]
3+
4+
auto trait AutoTrait {}
5+
6+
impl<T> !AutoTrait for [T] {}
7+
8+
fn needs_auto_trait<T: AutoTrait + ?Sized>() {}
9+
10+
fn main() {
11+
needs_auto_trait::<str>();
12+
//~^ ERROR the trait bound `[u8]: AutoTrait` is not satisfied in `str`
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: the trait bound `[u8]: AutoTrait` is not satisfied in `str`
2+
--> $DIR/str-contains-slice-conceptually.rs:11:22
3+
|
4+
LL | needs_auto_trait::<str>();
5+
| ^^^ within `str`, the trait `AutoTrait` is not implemented for `[u8]`
6+
|
7+
= note: required because it appears within the type `str`
8+
note: required by a bound in `needs_auto_trait`
9+
--> $DIR/str-contains-slice-conceptually.rs:8:24
10+
|
11+
LL | fn needs_auto_trait<T: AutoTrait + ?Sized>() {}
12+
| ^^^^^^^^^ required by this bound in `needs_auto_trait`
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)