Skip to content

Commit 7db08ee

Browse files
committed
Auto merge of #87250 - robojumper:87199-sized-relaxation, r=nikomatsakis
Fix implicit Sized relaxation when attempting to relax other, unsupported trait Fixes #87199. Do note that this bug fix causes code like the `ref_arg::<[i32]>(&[5]);` line in the test case in combination with an affected function to no longer compile.
2 parents 8024983 + 3dbe0ce commit 7db08ee

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
838838
this does nothing because the given bound is not \
839839
a default; only `?Sized` is supported",
840840
);
841+
return false;
841842
}
842843
}
843844
}

src/test/ui/issues/issue-87199.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Regression test for issue #87199, where attempting to relax a bound
2+
// other than the only supported `?Sized` would still cause the compiler
3+
// to assume that the `Sized` bound was relaxed.
4+
5+
// check-fail
6+
7+
// Check that these function definitions only emit warnings, not errors
8+
fn arg<T: ?Send>(_: T) {}
9+
//~^ warning: default bound relaxed for a type parameter, but this does nothing
10+
fn ref_arg<T: ?Send>(_: &T) {}
11+
//~^ warning: default bound relaxed for a type parameter, but this does nothing
12+
fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }
13+
//~^ warning: default bound relaxed for a type parameter, but this does nothing
14+
15+
// Check that there's no `?Sized` relaxation!
16+
fn main() {
17+
ref_arg::<i32>(&5);
18+
ref_arg::<[i32]>(&[5]);
19+
//~^ the size for values of type `[i32]` cannot be known
20+
}

src/test/ui/issues/issue-87199.stderr

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
2+
--> $DIR/issue-87199.rs:8:8
3+
|
4+
LL | fn arg<T: ?Send>(_: T) {}
5+
| ^
6+
7+
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
8+
--> $DIR/issue-87199.rs:10:12
9+
|
10+
LL | fn ref_arg<T: ?Send>(_: &T) {}
11+
| ^
12+
13+
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
14+
--> $DIR/issue-87199.rs:12:13
15+
|
16+
LL | fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
20+
--> $DIR/issue-87199.rs:18:22
21+
|
22+
LL | fn ref_arg<T: ?Send>(_: &T) {}
23+
| - required by this bound in `ref_arg`
24+
...
25+
LL | ref_arg::<[i32]>(&[5]);
26+
| ^^^^ doesn't have a size known at compile-time
27+
|
28+
= help: the trait `Sized` is not implemented for `[i32]`
29+
help: consider relaxing the implicit `Sized` restriction
30+
|
31+
LL | fn ref_arg<T: ?Send + ?Sized>(_: &T) {}
32+
| ^^^^^^^^
33+
34+
error: aborting due to previous error; 3 warnings emitted
35+
36+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)