]> git.lizzy.rs Git - rust.git/commitdiff
Mention fn coercion rules (needs to be expanded)
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 6 Jan 2023 20:15:10 +0000 (20:15 +0000)
committerEsteban Küber <esteban@kuber.com.ar>
Mon, 30 Jan 2023 21:51:33 +0000 (21:51 +0000)
27 files changed:
compiler/rustc_infer/src/infer/error_reporting/mod.rs
compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs
compiler/rustc_middle/src/ty/error.rs
tests/ui/argument-suggestions/two-mismatch-notes.stderr
tests/ui/associated-types/associated-types-eq-hr.stderr
tests/ui/associated-types/issue-87261.rs
tests/ui/associated-types/issue-87261.stderr
tests/ui/c-variadic/variadic-ffi-1.stderr
tests/ui/fn/fn-pointer-mismatch.stderr
tests/ui/fn/signature-error-reporting-under-verbose.rs
tests/ui/fn/signature-error-reporting-under-verbose.stderr
tests/ui/generic-associated-types/collections-project-default.stderr
tests/ui/generic-associated-types/issue-79422.extended.stderr
tests/ui/higher-rank-trait-bounds/issue-62203-hrtb-ice.stderr
tests/ui/impl-trait/equality2.stderr
tests/ui/issues/issue-10764.stderr
tests/ui/issues/issue-39970.rs
tests/ui/issues/issue-39970.stderr
tests/ui/mismatched_types/normalize-fn-sig.stderr
tests/ui/regions/region-lifetime-bounds-on-fns-where-clause.stderr
tests/ui/regions/region-multiple-lifetime-bounds-on-fns-where-clause.stderr
tests/ui/regions/regions-fn-subtyping-return-static-fail.stderr
tests/ui/regions/regions-lifetime-bounds-on-fns.stderr
tests/ui/reify-intrinsic.stderr
tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr
tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr
tests/ui/static/static-reference-to-fn-1.stderr

index 8ebcd3d54aa7ff7827b9980813a41b9f2d49f5ed..554a70265d43ce04b4fd84d806b0583750cecb45 100644 (file)
@@ -1865,6 +1865,18 @@ enum Similar<'tcx> {
         self.check_and_note_conflicting_crates(diag, terr);
 
         self.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());
+        if let Some(exp_found) = exp_found
+            && let exp_found = TypeError::Sorts(exp_found)
+            && exp_found != terr
+        {
+            self.note_and_explain_type_err(
+                diag,
+                exp_found,
+                cause,
+                span,
+                cause.body_id.to_def_id(),
+            );
+        }
 
         if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values
             && let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind()
index e18cfb93beddc4875747868ef784125ddbf4c0b0..39b3c98f0a5ccadd22b3e0ad79ad8c6c0472eccb 100644 (file)
@@ -218,6 +218,13 @@ fn foo(&self, x: T) -> T { x }
                             );
                         }
                     }
+                    (ty::FnPtr(_), ty::FnDef(def, _))
+                    if let hir::def::DefKind::Fn = tcx.def_kind(def) => {
+                        diag.note(
+                            "when the arguments and return types match, functions can be coerced \
+                             to function pointers",
+                        );
+                    }
                     _ => {}
                 }
                 debug!(
index 6229c0072f2a79f5397e7d869468e2852db5d2de..d188199773a5dc201f73a8c24dbda23435c1702f 100644 (file)
@@ -28,7 +28,7 @@ pub fn new(a_is_expected: bool, a: T, b: T) -> Self {
 }
 
 // Data structures used in type unification
-#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
+#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift, PartialEq, Eq)]
 #[rustc_pass_by_value]
 pub enum TypeError<'tcx> {
     Mismatch,
index 70cc60255c71759bc250ed3126abcd9d2e71eafd..38cf23ddc3895914a3f2dc037b85d18babae02fe 100644 (file)
@@ -11,6 +11,7 @@ LL |     foo(f, w);
    |         ^
    = note: expected fn pointer `fn(i32)`
                  found fn item `fn(u32) {f}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 note: expected `Wrapper<i32>`, found `Wrapper<isize>`
   --> $DIR/two-mismatch-notes.rs:10:12
    |
index 6cff403b318c193cd0e55e213debb9b6c772a1db..99db0c1bf3bc94ae388f30b49d4026796a0fd153 100644 (file)
@@ -1,8 +1,8 @@
-error[E0271]: type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
+error[E0271]: type mismatch resolving `<UintStruct as TheTrait<&isize>>::A == &isize`
   --> $DIR/associated-types-eq-hr.rs:87:11
    |
 LL |     foo::<UintStruct>();
-   |           ^^^^^^^^^^ type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
+   |           ^^^^^^^^^^ type mismatch resolving `<UintStruct as TheTrait<&isize>>::A == &isize`
    |
 note: expected this to be `&isize`
   --> $DIR/associated-types-eq-hr.rs:26:14
@@ -20,11 +20,11 @@ LL | where
 LL |     T: for<'x> TheTrait<&'x isize, A = &'x isize>,
    |                                    ^^^^^^^^^^^^^ required by this bound in `foo`
 
-error[E0271]: type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
+error[E0271]: type mismatch resolving `<IntStruct as TheTrait<&isize>>::A == &usize`
   --> $DIR/associated-types-eq-hr.rs:91:11
    |
 LL |     bar::<IntStruct>();
-   |           ^^^^^^^^^ type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
+   |           ^^^^^^^^^ type mismatch resolving `<IntStruct as TheTrait<&isize>>::A == &usize`
    |
 note: expected this to be `&usize`
   --> $DIR/associated-types-eq-hr.rs:14:14
index e8548d402fae5b1a7b4a8ffa57609d6853005922..384561f8ccd7e0496ef3c867ff4e015f8955a958 100644 (file)
@@ -77,10 +77,10 @@ fn check_generics<A, B, C, D, E, F, G>(a: A, b: B, c: C, d: D, e: E, f: F, g: G)
 
 fn main() {
     accepts_trait(returns_opaque());
-    //~^ ERROR type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`
+    //~^ ERROR type mismatch resolving `<impl Trait as Trait>::Associated == ()`
 
     accepts_trait(returns_opaque_derived());
-    //~^ ERROR type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`
+    //~^ ERROR type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
 
     accepts_trait(returns_opaque_foo());
     //~^ ERROR type mismatch resolving `<impl Trait + Foo as Trait>::Associated == ()`
@@ -89,7 +89,7 @@ fn main() {
     //~^ ERROR type mismatch resolving `<impl DerivedTrait + Foo as Trait>::Associated == ()`
 
     accepts_generic_trait(returns_opaque_generic());
-    //~^ ERROR type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`
+    //~^ ERROR type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
 
     accepts_generic_trait(returns_opaque_generic_foo());
     //~^ ERROR type mismatch resolving `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated == ()`
index 2cce6b947025e5b41c1361a5d555c33661dc46f8..f5c6d82471a8bd0967576990b76fa335e7479927 100644 (file)
@@ -132,7 +132,7 @@ note: required by a bound in `accepts_generic_trait`
 LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
    |                                              ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`
 
-error[E0271]: type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`
+error[E0271]: type mismatch resolving `<impl Trait as Trait>::Associated == ()`
   --> $DIR/issue-87261.rs:79:19
    |
 LL | fn returns_opaque() -> impl Trait + 'static {
@@ -155,7 +155,7 @@ help: consider constraining the associated type `<impl Trait + 'static as Trait>
 LL | fn returns_opaque() -> impl Trait<Associated = ()> + 'static {
    |                                  +++++++++++++++++
 
-error[E0271]: type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`
+error[E0271]: type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
   --> $DIR/issue-87261.rs:82:19
    |
 LL | fn returns_opaque_derived() -> impl DerivedTrait + 'static {
@@ -222,7 +222,7 @@ note: required by a bound in `accepts_trait`
 LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
    |                           ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
 
-error[E0271]: type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`
+error[E0271]: type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
   --> $DIR/issue-87261.rs:91:27
    |
 LL | fn returns_opaque_generic() -> impl GenericTrait<()> + 'static {
index 4beea83d8a5287b2a6216c24caa4ceb2fb157598..c7899338197d8e56f8ff7c165bd5fbcbe33d1108 100644 (file)
@@ -46,6 +46,7 @@ LL |         let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
    |
    = note: expected fn pointer `unsafe extern "C" fn(_, _)`
                  found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error[E0308]: mismatched types
   --> $DIR/variadic-ffi-1.rs:26:54
@@ -57,6 +58,7 @@ LL |         let y: extern "C" fn(f: isize, x: u8, ...) = bar;
    |
    = note: expected fn pointer `extern "C" fn(_, _, ...)`
                  found fn item `extern "C" fn(_, _) {bar}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error[E0617]: can't pass `f32` to variadic function
   --> $DIR/variadic-ffi-1.rs:28:19
index 74f79f6df5ab5c7c76da98200eca193cb0510662..bed59d4af6df1c2a06846b4c2d529789f5c1003b 100644 (file)
@@ -78,6 +78,7 @@ LL |     let e: &fn(u32) -> u32 = &foo;
    = note: expected reference `&fn(u32) -> u32`
               found reference `&fn(u32) -> u32 {foo}`
    = note: fn items are distinct from fn pointers
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to 6 previous errors
 
index d00cbd8a0f24298c581d5b8694595a9d5a5a3740..12ff113c913471fc6b7fd77fb34c2e8dd9295059 100644 (file)
@@ -12,4 +12,5 @@ fn main() {
     //~| NOTE expected fn pointer, found fn item
     //~| NOTE expected fn pointer `fn(i32, u32)`
     //~| NOTE arguments to this function are incorrect
+    //~| NOTE when the arguments and return types match, functions can be coerced to function pointers
 }
index 067ee824d5d84dd522887d4e9f34bb7ee2a87eb9..f4498db7259e37f07c277f923e7cc56130fb4d57 100644 (file)
@@ -8,6 +8,7 @@ LL |     needs_ptr(foo);
    |
    = note: expected fn pointer `fn(i32, u32)`
                  found fn item `fn(i32, i32) {foo}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 note: function defined here
   --> $DIR/signature-error-reporting-under-verbose.rs:5:4
    |
index 5701017dc3471adad820c7ec5268b5725cc2f13a..9aa0b239e4786f252dd0e4234422b76396ec73ff 100644 (file)
@@ -9,6 +9,7 @@ LL |     res
    |
    = note: expected associated type `<C as Collection<i32>>::Sibling<f32>`
               found associated type `<<C as Collection<i32>>::Family as CollectionFamily>::Member<f32>`
+   = note: an associated type was expected, but a different one was found
 
 error: aborting due to previous error
 
index d79de0ca62792cbffd112b529c527b094aaac760..04184fce921f2a66932abc312e7be4f57defa926 100644 (file)
@@ -14,11 +14,11 @@ help: add missing lifetime argument
 LL |         as Box<dyn MapLike<u8, u8, VRefCont<'a> = dyn RefCont<'_, u8>>>;
    |                                            ++++
 
-error[E0271]: type mismatch resolving `<BTreeMap<u8, u8> as MapLike<u8, u8>>::VRefCont<'_> == (dyn RefCont<'_, u8> + 'static)`
+error[E0271]: type mismatch resolving `<BTreeMap<u8, u8> as MapLike<u8, u8>>::VRefCont<'_> == dyn RefCont<'_, u8>`
   --> $DIR/issue-79422.rs:44:13
    |
 LL |     let m = Box::new(std::collections::BTreeMap::<u8, u8>::new())
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<BTreeMap<u8, u8> as MapLike<u8, u8>>::VRefCont<'_> == (dyn RefCont<'_, u8> + 'static)`
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<BTreeMap<u8, u8> as MapLike<u8, u8>>::VRefCont<'_> == dyn RefCont<'_, u8>`
    |
 note: expected this to be `(dyn RefCont<'_, u8> + 'static)`
   --> $DIR/issue-79422.rs:28:25
index 01d48ab59af1934c68219817bd3dfb9cbcd6e5fa..86198c3f7fd7945c374da3307fc622547c120a39 100644 (file)
@@ -1,4 +1,4 @@
-error[E0271]: type mismatch resolving `for<'r> <L<[closure@issue-62203-hrtb-ice.rs:42:16]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
+error[E0271]: type mismatch resolving `<L<[closure@issue-62203-hrtb-ice.rs:42:16]> as T0<'r, (&u8,)>>::O == <_ as Ty<'r>>::V`
   --> $DIR/issue-62203-hrtb-ice.rs:39:9
    |
 LL |       let v = Unit2.m(
@@ -10,7 +10,7 @@ LL | |             f: |x| {
 ...  |
 LL | |             },
 LL | |         },
-   | |_________^ type mismatch resolving `for<'r> <L<[closure@issue-62203-hrtb-ice.rs:42:16]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
+   | |_________^ type mismatch resolving `<L<[closure@issue-62203-hrtb-ice.rs:42:16]> as T0<'r, (&u8,)>>::O == <_ as Ty<'r>>::V`
    |
 note: expected this to be `<_ as Ty<'_>>::V`
   --> $DIR/issue-62203-hrtb-ice.rs:21:14
index e399a61023d1eebfcdcee33b06badb4e71b5ad63..52f25d2458c9190bc469f5efb1f99136970c8ae1 100644 (file)
@@ -54,6 +54,7 @@ LL |     x = (x.1,
    |
    = note: expected opaque type `impl Foo` (`u32`)
               found opaque type `impl Foo` (`i32`)
+   = note: distinct uses of `impl Trait` result in different opaque types
 
 error[E0308]: mismatched types
   --> $DIR/equality2.rs:41:10
@@ -69,6 +70,7 @@ LL |          x.0);
    |
    = note: expected opaque type `impl Foo` (`i32`)
               found opaque type `impl Foo` (`u32`)
+   = note: distinct uses of `impl Trait` result in different opaque types
 
 error: aborting due to 4 previous errors; 1 warning emitted
 
index 4d8a85a139715c8b52477ae56057f036d72e57da..fcb45affe2c18cc78ccfed3b3482ac18ced196af 100644 (file)
@@ -8,6 +8,7 @@ LL | fn main() { f(bar) }
    |
    = note: expected fn pointer `fn()`
                  found fn item `extern "C" fn() {bar}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 note: function defined here
   --> $DIR/issue-10764.rs:1:4
    |
index f51e3b522e1c57c4a9a3bfac1d61bfad8508dba6..40d3ae30a87c680811cdaad65b9969e1f56090d4 100644 (file)
@@ -17,5 +17,5 @@ impl Visit for () where
 
 fn main() {
     <() as Visit>::visit();
-    //~^ ERROR type mismatch resolving `for<'a> <() as Array<'a>>::Element == ()`
+    //~^ ERROR type mismatch resolving `<() as Array<'a>>::Element == ()`
 }
index 0cabdf7f234a8926078392340b1ac711dfcfd895..8344b88c3beab7e8e6863166be7d99286c12ddf9 100644 (file)
@@ -1,8 +1,8 @@
-error[E0271]: type mismatch resolving `for<'a> <() as Array<'a>>::Element == ()`
+error[E0271]: type mismatch resolving `<() as Array<'a>>::Element == ()`
   --> $DIR/issue-39970.rs:19:5
    |
 LL |     <() as Visit>::visit();
-   |     ^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `for<'a> <() as Array<'a>>::Element == ()`
+   |     ^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<() as Array<'a>>::Element == ()`
    |
 note: expected this to be `()`
   --> $DIR/issue-39970.rs:10:20
index 252e56387ba8b99eceb1508abcf5244728f72f6e..e3a0646550cb20c7f4e1a8006203fa56e0451cc6 100644 (file)
@@ -8,6 +8,7 @@ LL |     needs_i32_ref_fn(foo::<()>);
    |
    = note: expected fn pointer `fn(&'static i32, i32)`
                  found fn item `fn(i32, &'static i32) {foo::<()>}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 note: function defined here
   --> $DIR/normalize-fn-sig.rs:11:4
    |
index 3b62c7b61c1b1082d9d885a84ba49508622c53b5..bb5bc6f66a5f24db06667a1eabb8b863ae9cf911 100644 (file)
@@ -6,6 +6,7 @@ LL |     let _: fn(&mut &isize, &mut &isize) = a;
    |
    = note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
                  found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to previous error
 
index 8a18a234b5c4eb38de5a29bf49deeae19cbf2305..dbe9e9b1a2e56eb9de47d959f086d4a7a1645aba 100644 (file)
@@ -6,6 +6,7 @@ LL |     let _: fn(&mut &isize, &mut &isize, &mut &isize) = a;
    |
    = note: expected fn pointer `for<'a, 'b, 'c, 'd, 'e, 'f> fn(&'a mut &'b isize, &'c mut &'d isize, &'e mut &'f isize)`
                  found fn item `for<'a, 'b, 'c> fn(&'a mut &isize, &'b mut &isize, &'c mut &isize) {a::<'_, '_, '_>}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to previous error
 
index 8d82ff958ff3a9d4f80e6f83a8b4b37fa48b2bf3..766a3d0337c06019f71da037a035b743a784f9bb 100644 (file)
@@ -8,6 +8,7 @@ LL |     want_G(baz);
    |
    = note: expected fn pointer `for<'cx> fn(&'cx S) -> &'static S`
                  found fn item `for<'a> fn(&'a S) -> &'a S {baz}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 note: function defined here
   --> $DIR/regions-fn-subtyping-return-static-fail.rs:20:4
    |
index 17a9019436a1e0d6f8eed17babafe4a8227210fe..df0fd069edcad5a8bf02ba74d13b4fe6f97ba889 100644 (file)
@@ -6,6 +6,7 @@ LL |     let _: fn(&mut &isize, &mut &isize) = a;
    |
    = note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
                  found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to previous error
 
index 310b6c224e0e77decd21ddab61faa77db891a777..9f9034a30c736378396c010ffd5a1afc80da6291 100644 (file)
@@ -8,6 +8,7 @@ LL |     let _: unsafe extern "rust-intrinsic" fn(isize) -> usize = std::mem::tr
    |
    = note: expected fn pointer `unsafe extern "rust-intrinsic" fn(isize) -> usize`
                  found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
   --> $DIR/reify-intrinsic.rs:11:13
index 07f6dc906c6bbf9bc56c6231b2c1371768a3c08f..36561ab91ee89713a03a69d12dd797e47fca8762 100644 (file)
@@ -15,6 +15,7 @@ LL |     let foo: fn() = foo;
                  found fn item `fn() {foo}`
    = note: fn items are distinct from fn pointers
    = note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to previous error
 
index 07f6dc906c6bbf9bc56c6231b2c1371768a3c08f..36561ab91ee89713a03a69d12dd797e47fca8762 100644 (file)
@@ -15,6 +15,7 @@ LL |     let foo: fn() = foo;
                  found fn item `fn() {foo}`
    = note: fn items are distinct from fn pointers
    = note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to previous error
 
index ea4c7b41995e34254678c5aefccb2b34fd305138..a75c7438cb74ec1d29922d0ecb5d4453f7dd2f1a 100644 (file)
@@ -10,6 +10,7 @@ LL |         func: &foo,
    = note: expected reference `&fn() -> Option<isize>`
               found reference `&fn() -> Option<isize> {foo}`
    = note: fn items are distinct from fn pointers
+   = note: when the arguments and return types match, functions can be coerced to function pointers
 
 error: aborting due to previous error