re https://github.com/rust-lang/rust/pull/72380#discussion_r438289385
Given the toy code
```rust
fn is_positive(n: usize) {
n > -1_isize;
}
```
We currently get a type mismatch error like the following:
```
error[E0308]: mismatched types
--> src/main.rs:2:9
|
2 | n > -1_isize;
| ^^^^^^^^ expected `usize`, found `isize`
|
help: you can convert an `isize` to `usize` and panic if the converted value wouldn't fit
|
2 | n > (-1_isize).try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
But clearly, `-1` can never fit into a `usize`, so the suggestion will
always panic. A more useful message would tell the user that the value
can never fit in the expected type:
```
error[E0308]: mismatched types
--> test.rs:2:9
|
2 | n > -1_isize;
| ^^^^^^^^ expected `usize`, found `isize`
|
note: `-1_isize` can never fit into `usize`
--> test.rs:2:9
|
2 | n > -1_isize;
| ^^^^^^^^
```
Which is what this commit implements.
I only added this check for negative literals because
- Currently we can only perform such a check for literals (constant
value propagation is outside the scope of the typechecker at this
point)
- A lint error for out-of-range numeric literals is already emitted
IMO it makes more sense to put this check in librustc_lint, but as far
as I can tell the typecheck pass happens before the lint pass, so I've
added it here.
r? @estebank
Rollup of 8 pull requests
Successful merges:
- #72932 (Clarify the behaviour of Pattern when used with methods like str::contains)
- #73066 (Querify whether a type has structural equality (Take 2))
- #73194 (Prefer the associated constants for pattern matching error)
- #73241 (Add/update comments about MinGW late_link_args)
- #73267 (Use the built cargo for cargotest.)
- #73290 (Fix links when pinging notification groups)
- #73302 (Adjusted some doctests in libcore to use `should_panic`.)
- #73308 (pretty/asm.rs should only be tested for x86_64 and not AArch64)
Failed merges:
r? @ghost
Adjusted some doctests in libcore to use `should_panic`.
Fixes#73196 .
I grepped libstd and libcore for all the instances of this pattern that I could find, but its possible that I missed some of course. If anyone knows of any more, please let me know and I will add them to the PR.
Use the built cargo for cargotest.
cargotest was using the beta (bootstrap) cargo. This changes it so that it will use the locally built cargo. This is intended to provide a sort of smoke test to ensure Cargo is functional. This *shouldn't* have any real impact on the CI build time. The cargotest job also happens to run cargo's testsuite, so it should already be building cargo.
Note: This will fail until #73266 is merged.
Querify whether a type has structural equality (Take 2)
Alternative to #72177.
Unlike in #72177, this helper method works for all types, falling back to a query for `TyKind::Adt`s that determines whether the `{Partial,}StructuralEq` traits are implemented.
This is my preferred interface for this method. I think this is better than just documenting that the helper only works for ADTs. If others disagree, we can just merge #72177 with the fixes applied. This has already taken far too long.
Clarify the behaviour of Pattern when used with methods like str::contains
Fixes#45507.
I used the previous work by @Emerentius (thanks !), added a paragraph and checked the links (they work for me but I'm not against someone else checking them too).
Add needs-sanitizer-{address,leak,memory,thread} directive indicating
that test requires target with support for specific sanitizer.
This is an addition to the existing needs-sanitizer-support directive
indicating that test requires a sanitizer runtime library.
These lead to inference regressions (mostly in tests) in code that looks
like:
let socket = std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(127, 0, 0, 1), 8080);
assert_eq!(socket, "127.0.0.1:8080".parse().unwrap());
That compiles as of stable 1.44.0 but fails in beta with:
error[E0284]: type annotations needed
--> src/main.rs:3:41
|
3 | assert_eq!(socket, "127.0.0.1:8080".parse().unwrap());
| ^^^^^ cannot infer type for type parameter `F` declared on the associated function `parse`
|
= note: cannot satisfy `<_ as std::str::FromStr>::Err == _`
help: consider specifying the type argument in the method call
|
3 | assert_eq!(socket, "127.0.0.1:8080".parse::<F>().unwrap());
|
Previously, some doctests were spawning new threads and joining them to
indicate that a particular call should panic; this hurt readability, so
the tests have been adjusted to simply call the method and use the
`should_panic` marker.