Skip to content

Commit 5ae8e23

Browse files
committed
Mention fn coercion rules (needs to be expanded)
1 parent 153ed09 commit 5ae8e23

27 files changed

+56
-18
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18651865
self.check_and_note_conflicting_crates(diag, terr);
18661866

18671867
self.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());
1868+
if let Some(exp_found) = exp_found
1869+
&& let exp_found = TypeError::Sorts(exp_found)
1870+
&& exp_found != terr
1871+
{
1872+
self.note_and_explain_type_err(
1873+
diag,
1874+
exp_found,
1875+
cause,
1876+
span,
1877+
cause.body_id.to_def_id(),
1878+
);
1879+
}
18681880

18691881
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values
18701882
&& let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind()

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ impl<T> Trait<T> for X {
218218
);
219219
}
220220
}
221+
(ty::FnPtr(_), ty::FnDef(def, _))
222+
if let hir::def::DefKind::Fn = tcx.def_kind(def) => {
223+
diag.note(
224+
"when the arguments and return types match, functions can be coerced \
225+
to function pointers",
226+
);
227+
}
221228
_ => {}
222229
}
223230
debug!(

compiler/rustc_middle/src/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<T> ExpectedFound<T> {
2828
}
2929

3030
// Data structures used in type unification
31-
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
31+
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift, PartialEq, Eq)]
3232
#[rustc_pass_by_value]
3333
pub enum TypeError<'tcx> {
3434
Mismatch,

tests/ui/argument-suggestions/two-mismatch-notes.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | foo(f, w);
1111
| ^
1212
= note: expected fn pointer `fn(i32)`
1313
found fn item `fn(u32) {f}`
14+
= note: when the arguments and return types match, functions can be coerced to function pointers
1415
note: expected `Wrapper<i32>`, found `Wrapper<isize>`
1516
--> $DIR/two-mismatch-notes.rs:10:12
1617
|

tests/ui/associated-types/associated-types-eq-hr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0271]: type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
1+
error[E0271]: type mismatch resolving `<UintStruct as TheTrait<&isize>>::A == &isize`
22
--> $DIR/associated-types-eq-hr.rs:87:11
33
|
44
LL | foo::<UintStruct>();
5-
| ^^^^^^^^^^ type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
5+
| ^^^^^^^^^^ type mismatch resolving `<UintStruct as TheTrait<&isize>>::A == &isize`
66
|
77
note: expected this to be `&isize`
88
--> $DIR/associated-types-eq-hr.rs:26:14
@@ -20,11 +20,11 @@ LL | where
2020
LL | T: for<'x> TheTrait<&'x isize, A = &'x isize>,
2121
| ^^^^^^^^^^^^^ required by this bound in `foo`
2222

23-
error[E0271]: type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
23+
error[E0271]: type mismatch resolving `<IntStruct as TheTrait<&isize>>::A == &usize`
2424
--> $DIR/associated-types-eq-hr.rs:91:11
2525
|
2626
LL | bar::<IntStruct>();
27-
| ^^^^^^^^^ type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
27+
| ^^^^^^^^^ type mismatch resolving `<IntStruct as TheTrait<&isize>>::A == &usize`
2828
|
2929
note: expected this to be `&usize`
3030
--> $DIR/associated-types-eq-hr.rs:14:14

tests/ui/associated-types/issue-87261.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ where
7777

7878
fn main() {
7979
accepts_trait(returns_opaque());
80-
//~^ ERROR type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`
80+
//~^ ERROR type mismatch resolving `<impl Trait as Trait>::Associated == ()`
8181

8282
accepts_trait(returns_opaque_derived());
83-
//~^ ERROR type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`
83+
//~^ ERROR type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
8484

8585
accepts_trait(returns_opaque_foo());
8686
//~^ ERROR type mismatch resolving `<impl Trait + Foo as Trait>::Associated == ()`
@@ -89,7 +89,7 @@ fn main() {
8989
//~^ ERROR type mismatch resolving `<impl DerivedTrait + Foo as Trait>::Associated == ()`
9090

9191
accepts_generic_trait(returns_opaque_generic());
92-
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`
92+
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
9393

9494
accepts_generic_trait(returns_opaque_generic_foo());
9595
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated == ()`

tests/ui/associated-types/issue-87261.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ note: required by a bound in `accepts_generic_trait`
132132
LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
133133
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`
134134

135-
error[E0271]: type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`
135+
error[E0271]: type mismatch resolving `<impl Trait as Trait>::Associated == ()`
136136
--> $DIR/issue-87261.rs:79:19
137137
|
138138
LL | fn returns_opaque() -> impl Trait + 'static {
@@ -155,7 +155,7 @@ help: consider constraining the associated type `<impl Trait + 'static as Trait>
155155
LL | fn returns_opaque() -> impl Trait<Associated = ()> + 'static {
156156
| +++++++++++++++++
157157

158-
error[E0271]: type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`
158+
error[E0271]: type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
159159
--> $DIR/issue-87261.rs:82:19
160160
|
161161
LL | fn returns_opaque_derived() -> impl DerivedTrait + 'static {
@@ -222,7 +222,7 @@ note: required by a bound in `accepts_trait`
222222
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
223223
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
224224

225-
error[E0271]: type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`
225+
error[E0271]: type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
226226
--> $DIR/issue-87261.rs:91:27
227227
|
228228
LL | fn returns_opaque_generic() -> impl GenericTrait<()> + 'static {

tests/ui/c-variadic/variadic-ffi-1.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
4646
|
4747
= note: expected fn pointer `unsafe extern "C" fn(_, _)`
4848
found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
49+
= note: when the arguments and return types match, functions can be coerced to function pointers
4950

5051
error[E0308]: mismatched types
5152
--> $DIR/variadic-ffi-1.rs:26:54
@@ -57,6 +58,7 @@ LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
5758
|
5859
= note: expected fn pointer `extern "C" fn(_, _, ...)`
5960
found fn item `extern "C" fn(_, _) {bar}`
61+
= note: when the arguments and return types match, functions can be coerced to function pointers
6062

6163
error[E0617]: can't pass `f32` to variadic function
6264
--> $DIR/variadic-ffi-1.rs:28:19

tests/ui/fn/fn-pointer-mismatch.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ LL | let e: &fn(u32) -> u32 = &foo;
7878
= note: expected reference `&fn(u32) -> u32`
7979
found reference `&fn(u32) -> u32 {foo}`
8080
= note: fn items are distinct from fn pointers
81+
= note: when the arguments and return types match, functions can be coerced to function pointers
8182

8283
error: aborting due to 6 previous errors
8384

tests/ui/fn/signature-error-reporting-under-verbose.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ fn main() {
1212
//~| NOTE expected fn pointer, found fn item
1313
//~| NOTE expected fn pointer `fn(i32, u32)`
1414
//~| NOTE arguments to this function are incorrect
15+
//~| NOTE when the arguments and return types match, functions can be coerced to function pointers
1516
}

0 commit comments

Comments
 (0)