Skip to content

Commit

Permalink
Add explicit type annotations for vec.push(break) test
Browse files Browse the repository at this point in the history
This code was being correctly matched by the never-type fallback lint.

In this case, the fallback is actually safe - constructing a `Vec<!>` is
perfectly fine. However, the lint doesn't know that - without knowing
about the inner details of `Vec`, there's no way of knowing that
changing `Vec::<()>::new()` to `Vec::<!>::new()` is safe.

I think this should be sufficiently rare in practice as to not be an
issue. If a Vec<_> gets inferred to Vec<!> due to fallback, you must
have never pushed anything to it (or else fallback would not have run
due to the type variable being constrained).

If the error does come up, adding type annotations is sufficient to fix
the problem - using `Vec<()>`, `Vec<!> or (before the `!` type is stabilized)
`Vec<std::convert::Infallible`, or really any type, will work.

Alternatively, we might consider special-casing some (or all) safe methods in
`libcore` and `libstd`, as fallback should never cause an issue with
them.
  • Loading branch information
Aaron1011 committed Jan 19, 2020
1 parent 39af896 commit c82ea60
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-51345.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(unreachable_code)]

fn main() {
let mut v = Vec::new();
let mut v: Vec<()> = Vec::new();

loop { v.push(break) }
}

0 comments on commit c82ea60

Please sign in to comment.