Skip to content

Commit 8ee77f8

Browse files
committed
Do not use ill-formed input expectations when checking function calls
1 parent eb3fdb2 commit 8ee77f8

7 files changed

+25
-217
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
305305
})
306306
.ok()
307307
})
308-
.unwrap_or_default();
308+
.unwrap_or_default()
309+
.filter(|expected_input_tys: &Vec<Ty<'_>>| {
310+
// Check the well-formedness of expected input tys, as using ill-formed
311+
// expectation may cause type inference errors.
312+
self.probe(|_| {
313+
let ocx = ObligationCtxt::new(self);
314+
for &ty in expected_input_tys {
315+
ocx.register_obligation(traits::Obligation::new(
316+
self.tcx,
317+
self.misc(call_span),
318+
self.param_env,
319+
ty::ClauseKind::WellFormed(ty.into()),
320+
));
321+
}
322+
ocx.try_evaluate_obligations().is_empty()
323+
})
324+
});
309325

310326
let mut err_code = E0061;
311327

tests/ui/coercion/fudge-inference/fn-ret-trait-object-propagated-to-inputs-issue-149379-1.current.stderr

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/ui/coercion/fudge-inference/fn-ret-trait-object-propagated-to-inputs-issue-149379-1.next.stderr

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//@ revisions: current next
22
//@ ignore-compare-mode-next-solver (explicit revisions)
33
//@[next] compile-flags: -Znext-solver
4+
//@ check-pass
45

5-
// FIXME(#149379): This should pass, but fails due to fudged expactation
6-
// types which are potentially not well-formed or for whom the function
7-
// where-bounds don't actually hold. And this results in weird bugs when
8-
// later treating these expectations as if they were actually correct..
6+
// A regression test for https://github.com/rust-lang/rust/issues/149379.
97

108
fn foo<T>(x: (T, ())) -> Box<T> {
119
Box::new(x.0)
@@ -14,6 +12,4 @@ fn foo<T>(x: (T, ())) -> Box<T> {
1412
fn main() {
1513
// Uses expectation as its struct tail is sized, resulting in `(dyn Send, ())`
1614
let _: Box<dyn Send> = foo(((), ()));
17-
//~^ ERROR mismatched types
18-
//~| ERROR the size for values of type `dyn Send` cannot be known at compilation time
1915
}
Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,5 @@
1-
error[E0277]: the size for values of type `dyn Send` cannot be known at compilation time
2-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:32:55
3-
|
4-
LL | let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
5-
| ^ doesn't have a size known at compile-time
6-
|
7-
= help: the trait `Sized` is not implemented for `dyn Send`
8-
note: required by an implicit `Sized` bound in `Foo`
9-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:10:12
10-
|
11-
LL | struct Foo<T> {
12-
| ^ required by the implicit `Sized` requirement on this type parameter in `Foo`
13-
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
14-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:10:12
15-
|
16-
LL | struct Foo<T> {
17-
| ^ this could be changed to `T: ?Sized`...
18-
LL | field: T,
19-
| - ...if indirection were used here: `Box<T>`
20-
21-
error[E0308]: mismatched types
22-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:32:55
23-
|
24-
LL | let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
25-
| ^ expected trait object, found integer
26-
|
27-
= note: expected trait object `dyn Send`
28-
found type `{integer}`
29-
30-
error[E0277]: the size for values of type `dyn Send` cannot be known at compilation time
31-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:32:42
32-
|
33-
LL | let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
34-
| ------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
35-
| |
36-
| required by a bound introduced by this call
37-
|
38-
= help: the trait `Sized` is not implemented for `dyn Send`
39-
note: required by an implicit `Sized` bound in `field_to_box1`
40-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:19:18
41-
|
42-
LL | fn field_to_box1<T>(x: Foo<T>) -> Box<T> {
43-
| ^ required by the implicit `Sized` requirement on this type parameter in `field_to_box1`
44-
help: consider relaxing the implicit `Sized` restriction
45-
|
46-
LL | fn field_to_box1<T: ?Sized>(x: Foo<T>) -> Box<T> {
47-
| ++++++++
48-
49-
error[E0277]: the size for values of type `dyn Send` cannot be known at compilation time
50-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:36:38
51-
|
52-
LL | let _: &dyn Send = field_to_box2(&Bar { field: 1 });
53-
| ------------- ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
54-
| |
55-
| required by a bound introduced by this call
56-
|
57-
= help: the trait `Sized` is not implemented for `dyn Send`
58-
note: required by an implicit `Sized` bound in `field_to_box2`
59-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:23:18
60-
|
61-
LL | fn field_to_box2<T>(x: &Bar<T>) -> &T {
62-
| ^ required by the implicit `Sized` requirement on this type parameter in `field_to_box2`
63-
help: consider relaxing the implicit `Sized` restriction
64-
|
65-
LL | fn field_to_box2<T: ?Sized>(x: &Bar<T>) -> &T {
66-
| ++++++++
67-
681
error[E0308]: mismatched types
69-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:38:38
2+
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:34:38
703
|
714
LL | let _: &dyn Send = field_to_box3(&(1,));
725
| ------------- ^^^^^ expected `&(dyn Send,)`, found `&({integer},)`
@@ -81,7 +14,6 @@ note: function defined here
8114
LL | fn field_to_box3<T>(x: &(T,)) -> &T {
8215
| ^^^^^^^^^^^^^ --------
8316

84-
error: aborting due to 5 previous errors
17+
error: aborting due to 1 previous error
8518

86-
Some errors have detailed explanations: E0277, E0308.
87-
For more information about an error, try `rustc --explain E0277`.
19+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,5 @@
1-
error[E0277]: the size for values of type `dyn Send` cannot be known at compilation time
2-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:32:55
3-
|
4-
LL | let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
5-
| ^ doesn't have a size known at compile-time
6-
|
7-
= help: the trait `Sized` is not implemented for `dyn Send`
8-
note: required by an implicit `Sized` bound in `Foo`
9-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:10:12
10-
|
11-
LL | struct Foo<T> {
12-
| ^ required by the implicit `Sized` requirement on this type parameter in `Foo`
13-
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
14-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:10:12
15-
|
16-
LL | struct Foo<T> {
17-
| ^ this could be changed to `T: ?Sized`...
18-
LL | field: T,
19-
| - ...if indirection were used here: `Box<T>`
20-
21-
error[E0308]: mismatched types
22-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:32:55
23-
|
24-
LL | let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
25-
| ^ expected trait object, found integer
26-
|
27-
= note: expected trait object `dyn Send`
28-
found type `{integer}`
29-
30-
error[E0277]: the size for values of type `dyn Send` cannot be known at compilation time
31-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:32:42
32-
|
33-
LL | let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
34-
| ------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
35-
| |
36-
| required by a bound introduced by this call
37-
|
38-
= help: the trait `Sized` is not implemented for `dyn Send`
39-
note: required by an implicit `Sized` bound in `field_to_box1`
40-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:19:18
41-
|
42-
LL | fn field_to_box1<T>(x: Foo<T>) -> Box<T> {
43-
| ^ required by the implicit `Sized` requirement on this type parameter in `field_to_box1`
44-
help: consider relaxing the implicit `Sized` restriction
45-
|
46-
LL | fn field_to_box1<T: ?Sized>(x: Foo<T>) -> Box<T> {
47-
| ++++++++
48-
49-
error[E0277]: the size for values of type `dyn Send` cannot be known at compilation time
50-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:36:38
51-
|
52-
LL | let _: &dyn Send = field_to_box2(&Bar { field: 1 });
53-
| ------------- ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
54-
| |
55-
| required by a bound introduced by this call
56-
|
57-
= help: the trait `Sized` is not implemented for `dyn Send`
58-
note: required by an implicit `Sized` bound in `field_to_box2`
59-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:23:18
60-
|
61-
LL | fn field_to_box2<T>(x: &Bar<T>) -> &T {
62-
| ^ required by the implicit `Sized` requirement on this type parameter in `field_to_box2`
63-
help: consider relaxing the implicit `Sized` restriction
64-
|
65-
LL | fn field_to_box2<T: ?Sized>(x: &Bar<T>) -> &T {
66-
| ++++++++
67-
681
error[E0308]: mismatched types
69-
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:38:38
2+
--> $DIR/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs:34:38
703
|
714
LL | let _: &dyn Send = field_to_box3(&(1,));
725
| ------------- ^^^^^ expected `&(dyn Send,)`, found `&({integer},)`
@@ -81,7 +14,6 @@ note: function defined here
8114
LL | fn field_to_box3<T>(x: &(T,)) -> &T {
8215
| ^^^^^^^^^^^^^ --------
8316

84-
error: aborting due to 5 previous errors
17+
error: aborting due to 1 previous error
8518

86-
Some errors have detailed explanations: E0277, E0308.
87-
For more information about an error, try `rustc --explain E0277`.
19+
For more information about this error, try `rustc --explain E0308`.

tests/ui/coercion/fudge-inference/fn-ret-trait-object-propagated-to-inputs-issue-149379-3.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ fn field_to_box3<T>(x: &(T,)) -> &T {
3030

3131
fn main() {
3232
let _: Box<dyn Send> = field_to_box1(Foo { field: 1, tail: () });
33-
//~^ ERROR the size for values of type `dyn Send` cannot be known at compilation time
34-
//~| ERROR the size for values of type `dyn Send` cannot be known at compilation time
35-
//~| ERROR mismatched types
3633
let _: &dyn Send = field_to_box2(&Bar { field: 1 });
37-
//~^ ERROR the size for values of type `dyn Send` cannot be known at compilation time
3834
let _: &dyn Send = field_to_box3(&(1,));
3935
//~^ ERROR mismatched types
4036
}

0 commit comments

Comments
 (0)