Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully-qualifying a From::from call adds an implicit reborrow that is not inferred otherwise #89966

Open
camelid opened this issue Oct 16, 2021 · 3 comments
Labels
A-borrow-checker Area: The borrow checker A-inference Area: Type inference A-type-system Area: Type system C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@camelid
Copy link
Member

camelid commented Oct 16, 2021

This code fails with a borrow-check error:

struct Foo;

impl<'a> From<&'a mut Foo> for () {
    fn from(_: &'a mut Foo) -> () {}
}

fn main() {
    let mut foo = Foo;
    let foo_mut = &mut foo;

    <()>::from(foo_mut);
    <()>::from(foo_mut);
}
error[E0382]: use of moved value: `foo_mut`
  --> src/main.rs:12:16
   |
9  |     let foo_mut = &mut foo;
   |         ------- move occurs because `foo_mut` has type `&mut Foo`, which does not implement the `Copy` trait
10 | 
11 |     <()>::from(foo_mut);
   |                ------- value moved here
12 |     <()>::from(foo_mut);
   |                ^^^^^^^ value used here after move

For more information about this error, try `rustc --explain E0382`.

However, explicitly specifying <() as From<&mut Foo>> in the first call causes compilation to succeed:

struct Foo;

impl<'a> From<&'a mut Foo> for () {
    fn from(_: &'a mut Foo) -> () {}
}

fn main() {
    let mut foo = Foo;
    let foo_mut = &mut foo;

    <() as From<&mut Foo>>::from(foo_mut);
    <()>::from(foo_mut);
}

Comparing the MIR output for the two versions, it appears that the fully-qualified version reborrows from foo_mut, whereas the unqualified version does not:

     bb0: {
-        StorageLive(_1);                 // scope 0 at unqual.rs:10:9: 10:16
-        _1 = Foo;                        // scope 0 at unqual.rs:10:19: 10:22
-        FakeRead(ForLet(None), _1);      // scope 0 at unqual.rs:10:9: 10:16
-        StorageLive(_2);                 // scope 1 at unqual.rs:11:9: 11:16
-        _2 = &mut _1;                    // scope 1 at unqual.rs:11:19: 11:27
-        FakeRead(ForLet(None), _2);      // scope 1 at unqual.rs:11:9: 11:16
-        StorageLive(_3);                 // scope 2 at unqual.rs:13:5: 13:24
-        StorageLive(_4);                 // scope 2 at unqual.rs:13:16: 13:23
-        _4 = move _2;                    // scope 2 at unqual.rs:13:16: 13:23
-        _3 = <() as From<&mut Foo>>::from(move _4) -> [return: bb1, unwind: bb3]; // scope 2 at unqual.rs:13:5: 13:24
+        StorageLive(_1);                 // scope 0 at fqs.rs:10:9: 10:16
+        _1 = Foo;                        // scope 0 at fqs.rs:10:19: 10:22
+        FakeRead(ForLet(None), _1);      // scope 0 at fqs.rs:10:9: 10:16
+        StorageLive(_2);                 // scope 1 at fqs.rs:11:9: 11:16
+        _2 = &mut _1;                    // scope 1 at fqs.rs:11:19: 11:27
+        FakeRead(ForLet(None), _2);      // scope 1 at fqs.rs:11:9: 11:16
+        StorageLive(_3);                 // scope 2 at fqs.rs:13:5: 13:42
+        StorageLive(_4);                 // scope 2 at fqs.rs:13:34: 13:41
+        _4 = &mut (*_2);                 // scope 2 at fqs.rs:13:34: 13:41
+        _3 = <() as From<&mut Foo>>::from(move _4) -> [return: bb1, unwind: bb3]; // scope 2 at fqs.rs:13:5: 13:42
                                          // mir::Constant
-                                         // + span: unqual.rs:13:5: 13:15
+                                         // + span: fqs.rs:13:5: 13:33
                                          // + user_ty: UserType(0)
                                          // + literal: Const { ty: fn(&mut Foo) {<() as std::convert::From<&mut Foo>>::from}, val: Value(Scalar(<ZST>)) }
     }

I'm guessing this reborrow is due to, or at least related to, the following change in "User Type Annotations":

 | User Type Annotations
-| 0: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], value: TypeOf(DefId(2:2912 ~ core[f488]::convert::From::from), UserSubsts { substs: [(), ^0], user_self_ty: None }) } at unqual.rs:13:5: 13:15
-| 1: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], value: TypeOf(DefId(2:2912 ~ core[f488]::convert::From::from), UserSubsts { substs: [(), ^0], user_self_ty: None }) } at unqual.rs:14:5: 14:15
+| 0: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], value: TypeOf(DefId(2:2912 ~ core[f488]::convert::From::from), UserSubsts { substs: [(), &mut Foo], user_self_ty: None }) } at fqs.rs:13:5: 13:33
+| 1: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], value: TypeOf(DefId(2:2912 ~ core[f488]::convert::From::from), UserSubsts { substs: [(), ^0], user_self_ty: None }) } at fqs.rs:14:5: 14:15

In the unqualified <()>::from(...) version, From::from's type seems to be generic over a type parameter, while in the qualified <() as From<&mut Foo>>::from(...) version, From::from's type is generic over a lifetime.

See also this Zulip thread.

@camelid camelid added A-type-system Area: Type system A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference C-discussion Category: Discussion or questions that doesn't represent real issues. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Oct 16, 2021
@camelid
Copy link
Member Author

camelid commented Oct 16, 2021

(I'm not sure if this is a bug or not, so I used the label C-discussion instead.)

@SkiFire13
Copy link
Contributor

I think this is due to the fact that the compiler doesn't perform coercions in generic contexts, and the argument of <()>::from is such context because the T in the From<T> used in the call is unknown. This in turn means that subtyping coercion and thus reborrowing is not automatically performed. This doesn't happen with <() as From<&mut Foo>> because that T is known, thus the compiler can coerce the argument.

This behaviour seems consistent with the current documented rules, although it's questionable whether that rules are what we actually want.

See rust-lang/reference#788 and the nomicon page on coercions for more informations.

@mbartlett21
Copy link
Contributor

The same happens if you decide to drop a mutable reference:

let a = &mut 5;
drop(a);
*a += 1; // error

And it works if you just specify that it is a reference:

drop::<&mut _>(a) // works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-inference Area: Type inference A-type-system Area: Type system C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants