about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2019-02-13 18:01:37 +0100
committerSimon Sapin <simon.sapin@exyr.org>2019-02-13 19:29:07 +0100
commitb2cf9a02b2bce6ed984b0c967e2daf0af7b13629 (patch)
treeb7870f3283d4b04b9a2ea551be6d52bfc99b5c18
parentc80a8f51dcdc90dd8a5234f3bef6160814eee5df (diff)
downloadrust-b2cf9a02b2bce6ed984b0c967e2daf0af7b13629.tar.gz
rust-b2cf9a02b2bce6ed984b0c967e2daf0af7b13629.zip
Add `impl From<!> for Infallible`
The reverse conversion unfortunately causes unexpected errors like:

```
error[E0277]: the trait bound `!: std::convert::From<()>` is not satisfied
   --> src/librustc_metadata/encoder.rs:105:9
    |
105 |         self.emit_usize(seq.len)?;
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<()>` is not implemented for `!`
    |
    = help: the following implementations were found:
              <! as std::convert::From<std::convert::Infallible>>
    = note: the trait is implemented for `()`. Possibly this error has been caused by changes to Rust's type-inference algorithm (see: https://github.com/rust-lang/rust/issues/48950 for more info). Consider whether you meant to use the type `()` here instead.
    = note: required by `std::convert::From::from`
```

I don’t understand why this error happens.
If I’m reading the code correctly the return types of `emit_usize`
and of the method that contains line 105 are both `Result<(), !>`,
so the expansion of the `?` operator should involve `!: From<!>`,
not `From<()>`.

Is this a type inference bug?
-rw-r--r--src/libcore/convert.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs
index 65aa91e2dab..c6c41fb99a4 100644
--- a/src/libcore/convert.rs
+++ b/src/libcore/convert.rs
@@ -596,3 +596,10 @@ impl PartialEq for Infallible {
 
 #[stable(feature = "convert_infallible", since = "1.34.0")]
 impl Eq for Infallible {}
+
+#[stable(feature = "convert_infallible", since = "1.34.0")]
+impl From<!> for Infallible {
+    fn from(x: !) -> Self {
+        x
+    }
+}