The `try_coerce` method coerces from a source to a target
type, possibly inserting adjustments. It should guarantee
that the post-adjustment type is a subtype of the target type
(or else that some side-constraint has been registered which will lead
to an error). However, it used to return the (possibly adjusted) source
as the type of the expression rather than the target. This led to
less good downstream errors.
To work around this, the code around blocks -- and particular tail
expressions in blocks -- had some special case manipulation. However,
since that code is now using the more general `CoerceMany` construct (to
account for breaks), it can no longer take advantage of that. This lead
to some regressions in compile-fail tests were errors were reported at
"less good" locations than before.
This change modifies coercions to return the target type when successful
rather the source type. This extends the behavior from blocks to all
coercions. Typically this has limited effect but on a few tests yielded
better errors results (and avoided regressions, of course).
This change also restores the hint about removing semicolons which went
missing (by giving 'force-unit' coercions a chance to add notes etc).
For the most part, the current code performs similarly, although it
differs in some particulars. I'll be nice to have these tests for
judging future changes, as well.
First, we keep a `CoerceMany` now to find the LUB of all the break
expressions. Second, this `CoerceMany` is actually an
`Option<CoerceMany>`, and we store `None` for loops where "break with an
expression" is disallowed. This avoids silly duplicate errors about a
type mismatch, since the loops pass already reports an error that the
break cannot have an expression. Finally, since we now detect an invalid
break target during HIR lowering, refactor `find_loop` to be infallible.
Adjust tests as needed:
- some spans from breaks are slightly different
- break up a single loop into multiple since `CoerceMany` silences
redundant and derived errors
- add a ui test that we only give on error for loop-break-value
Instead, wait until coercion time. This has some small effects on a few
tests (one less temporary, generally better errors when trying to call
methods or otherwise "force" the type).
`match { }` now (correctly?) indicates divergence, which results in more
unreachable warnings. We also avoid fallback to `!` if there is just one
arm (see new test: `match-unresolved-one-arm.rs`).
The new strategy is as follows. First, the `!` type is assigned
in two cases:
- a block with a diverging statement and no tail expression (e.g.,
`{return;}`);
- any expression with the type `!` is considered diverging.
Second, we track when we are in a diverging state, and we permit a value
of any type to be coerced **into** `!` if the expression that produced
it is diverging. This means that `fn foo() -> ! { panic!(); 22 }`
type-checks, even though the block has a type of `usize`.
Finally, coercions **from** the `!` type to any other are always
permitted.
Fixes#39808.
In master, this field was an arbitrary node-id (in fact, an id for
something that doesn't even exist in the HIR -- the `catch` node).
Breaks targeting this block used that id. In the newer system, this
field is a boolean, and any breaks targeted this block will use the
id of the block.
macros: improve `Span`'s expansion information
This PR improves `Span`'s expansion information. More specifically:
- It refactors AST node span construction to preserve expansion information.
- Today, we only use the underlying tokens' `BytePos`s, throwing away the `ExpnId`s.
- This improves the accuracy of AST nodes' expansion information, fixing #30506.
- It refactors `span.expn_id: ExpnId` to `span.ctxt: SyntaxContext` and removes `ExpnId`.
- This gives all tokens as much hygiene information as `Ident`s.
- This is groundwork for procedural macros 2.0 `TokenStream` API.
- This is also groundwork for declarative macros 2.0, which will need this hygiene information for some non-`Ident` tokens.
- It simplifies processing of spans' expansion information throughout the compiler.
- It fixes#40649.
- It fixes#39450 and fixes part of #23480.
r? @nrc
libcore: fix compilation on 16bit target (MSP430).
Since PR #40601 has been merged, libcore no longer compiles on MSP430.
The reason is this code in `break_patterns`:
```rust
let mut random = len;
random ^= random << 13;
random ^= random >> 17;
random ^= random << 5;
random &= modulus - 1;
```
It assumes that `len` is at least a 32 bit integer.
As a workaround replace `break_patterns` with an empty function for 16bit targets.
cc @stjepang
cc @alexcrichton
Clarify suggetion for field used as method
Instead of
```rust
error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope
--> src/wire/ipv4.rs:409:34
|
409 | packet.set_src_addr(self.src_addr());
| ^^^^^^^^
|
note: did you mean to write `self.src_addr`?
--> src/wire/ipv4.rs:409:34
|
409 | packet.set_src_addr(self.src_addr());
| ^^^^^^^^
```
present
```rust
error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope
--> src/wire/ipv4.rs:409:34
|
409 | packet.set_src_addr(self.src_addr());
| ^^^^^^^^ field, not a method
|
= help: did you mean to write `self.src_addr` instead of `self.src_addr(...)`?
```
Fix#38321.