Commit Graph

13825 Commits

Author SHA1 Message Date
Yuki Okushi
afa2edbe42
Rollup merge of #95860 - c410-f3r:stabilize-meta, r=joshtriplett
Stabilize `$$` in Rust 1.63.0

# Stabilization proposal

This PR proposes the stabilization of a subset of `#![feature(macro_metavar_expr)]` or more specifically, the stabilization of dollar-dollar (`$$`).

Tracking issue: #83527
Version: 1.63 (2022-06-28 => beta, 2022-08-11 => stable).

## What is stabilized

```rust
macro_rules! foo {
    () => {
        macro_rules! bar {
            ( $$( $$any:tt )* ) => { $$( $$any )* };
        }
    };
}

fn main() {
    foo!();
}
```

## Motivation

For more examples, see the [RFC](https://github.com/markbt/rfcs/blob/macro_metavar_expr/text/0000-macro-metavar-expr.md).

Users must currently resort to a tricky and not so well-known hack to declare nested macros with repetitions.

```rust
macro_rules! foo {
    ($dollar:tt) => {
        macro_rules! bar {
            ( $dollar ( $any:tt )* ) => { $dollar ( $any )* };
        }
    };
}
fn main() {
    foo!($);
}
```

As seen above, such hack is fragile and makes work with declarative macros much more unpleasant. Dollar-dollar (`$$`), on the other hand, makes nested macros more intuitive.

## What isn't stabilized

`count`, `ignore`, `index` and `length` are not being stabilized due to the lack of consensus.

## History

* 2021-02-22, [RFC: Declarative macro metavariable expressions](https://github.com/rust-lang/rfcs/pull/3086)
* 2021-03-26, [Tracking Issue for RFC 3086: macro metavariable expressions](https://github.com/rust-lang/rust/issues/83527)
* 2022-02-01, [Implement macro meta-variable expressions](https://github.com/rust-lang/rust/pull/93545)
* 2022-02-25, [[1/2] Implement macro meta-variable expressions](https://github.com/rust-lang/rust/pull/94368)
* 2022-03-11, [[2/2] Implement macro meta-variable expressions](https://github.com/rust-lang/rust/pull/94833)
* 2022-03-12, [Fix remaining meta-variable expression TODOs](https://github.com/rust-lang/rust/pull/94884)
* 2019-03-21, [[macro-metavar-expr] Fix generated tokens hygiene](https://github.com/rust-lang/rust/pull/95188)
* 2022-04-07, [Kickstart the inner usage of macro_metavar_expr](https://github.com/rust-lang/rust/pull/95761)
* 2022-04-07, [[macro_metavar_expr] Add tests to ensure the feature requirement](https://github.com/rust-lang/rust/pull/95764)

## Non-stabilized expressions

https://github.com/rust-lang/rust/issues/83527 lists several concerns about some characteristics of `count`, `index` and `length` that effectively make their stabilization unfeasible. `$$` and `ignore`, however, are not part of any discussion and thus are suitable for stabilization.

It is not in the scope of this PR to detail each concern or suggest any possible converging solution. Such thing should be restrained in this tracking issue.

## Tests

This list is a subset of https://github.com/rust-lang/rust/tree/master/src/test/ui/macros/rfc-3086-metavar-expr

* [Ensures that nested macros have correct behavior](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/dollar-dollar-has-correct-behavior.rs)

* [Compares produced tokens to assert expected outputs](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/feature-gate-macro_metavar_expr.rs)

* [Checks the declarations of the feature](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/required-feature.rs)

* [Verifies all possible errors that can occur due to incorrect user input](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/syntax-errors.rs)

## Possible future work

Once consensus is achieved, other nightly expressions can be stabilized.

Thanks ``@markbt`` for creating the RFC and thanks to ``@petrochenkov`` and ``@mark-i-m`` for reviewing the implementations.
2022-06-09 19:19:55 +09:00
bors
6dc598a01b Auto merge of #97862 - SparrowLii:superset, r=lcnr
optimize `superset` method of `IntervalSet`

Given that intervals in the `IntervalSet` are sorted and strictly separated( it means the `end` of the previous interval will not be equal to the `start` of the next interval), we can reduce the complexity of the `superset` method from O(NMlogN) to O(2N) (N is the number of intervals and M is the length of each interval)
2022-06-09 07:13:46 +00:00
bors
282445a288 Auto merge of #97740 - RalfJung:ctfe-cycle-spans, r=lcnr
use precise spans for recursive const evaluation

This fixes https://github.com/rust-lang/rust/issues/73283 by using a `TyCtxtAt` with a more precise span when the interpreter recursively calls itself. Hopefully such calls are sufficiently rare that this does not cost us too much performance.

(In theory, cycles can also arise through layout computation, as layout can depend on consts -- but layout computation happens all the time so we'd have to do something to not make this terrible for performance.)
2022-06-09 01:52:15 +00:00
Michael Goulet
f12a1c23bc
Rollup merge of #97857 - ChayimFriedman2:box-identifier-help, r=compiler-errors
Suggest escaping `box` as identifier

Fixes #97810.
2022-06-08 13:32:21 -07:00
Michael Goulet
e0409200d9
Rollup merge of #97856 - compiler-errors:bad-let-suggestions, r=estebank
Don't suggest adding `let` in certain `if` conditions

Avoid being too eager to suggest `let` in an `if` condition with an `=`, namely when the LHS of the `=` isn't even valid as a pattern (to a first degree approximation).

This heustic I came up with kinda sucks. Let me know if it needs to be refined.
2022-06-08 13:32:20 -07:00
Michael Goulet
1922f0b980
Rollup merge of #97557 - compiler-errors:arg-mismatch-mini, r=jackh726
Fix indices and remove some unwraps in arg mismatch algorithm

This is a more conservative fix than #97542, addressing some indices which were used incorectly and unwraps which are bound to panic (e.g. when the provided and expected arg counts differ). Beta nominating this as it's quite easy to cause ICEs -- I wrote a fuzzer and found hundreds of examples of ICEs.

cc `@jackh726` as author of #92364, and `@estebank` as reviewer of that PR.
fixes #97484
r? `@jackh726` this should be _much_ easier to review than the other PR 😅
2022-06-08 13:32:18 -07:00
Matthias Krüger
c374529cb5
Rollup merge of #97880 - ChayimFriedman2:patch-2, r=lcnr
Fix typo: fo->for
2022-06-08 18:15:06 +02:00
Matthias Krüger
8a2aedc6e3
Rollup merge of #97813 - antoyo:sync_from_cg_gcc, r=petrochenkov
Sync rustc_codegen_gcc
2022-06-08 18:15:02 +02:00
SparrowLii
726b35bd70 correct the test if IntervalSet 2022-06-08 22:44:26 +08:00
SparrowLii
65a5b082bc fix the impl error in insert_all 2022-06-08 22:09:26 +08:00
Chayim Refael Friedman
f4ba14d290
Fix typo: fo->for 2022-06-08 16:40:02 +03:00
SparrowLii
7e1901537c add check_invariants method 2022-06-08 21:39:04 +08:00
bors
09d52bc5d4 Auto merge of #97873 - Dylan-DPC:rollup-g6ptsdq, r=Dylan-DPC
Rollup of 5 pull requests

Successful merges:

 - #97276 (Stabilize `const_intrinsic_copy`)
 - #97763 (Allow ptr_from_addr_cast to fail)
 - #97846 (Specify DWARF alignment in bits, not bytes.)
 - #97848 (Impl Traits lowering minor refactors)
 - #97865 (remove `BorrowckMode`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-06-08 13:20:45 +00:00
Dylan DPC
5d1a3669ee
Rollup merge of #97865 - lcnr:bb-BorrowckMode, r=Dylan-DPC
remove `BorrowckMode`

dead code after #95565
2022-06-08 13:43:21 +02:00
Dylan DPC
770583cc6d
Rollup merge of #97848 - spastorino:universal-lowering-refactor-1, r=cjgillot
Impl Traits lowering minor refactors

This are unrelated changes on my RPIT refactor that may be better to merge before opening the main PR.

r? `@cjgillot`

cc `@nikomatsakis`
2022-06-08 13:43:20 +02:00
Dylan DPC
82a1d79dff
Rollup merge of #97846 - pcwalton:align-bits, r=michaelwoerister
Specify DWARF alignment in bits, not bytes.

In DWARF, alignment of types is specified in bits, as is made clear by the
parameter name `AlignInBits`. However, `rustc` was incorrectly passing a byte
alignment. This commit fixes that.

This was noticed in upstream LLVM when I tried to check in a test consisting of
LLVM IR generated from `rustc` and it triggered assertions [1].

[1]: https://reviews.llvm.org/D126835
2022-06-08 13:43:19 +02:00
Dylan DPC
29c6f5f603
Rollup merge of #97763 - RalfJung:fallible-cast, r=lcnr
Allow ptr_from_addr_cast to fail

This is needed for https://github.com/rust-lang/miri/issues/2133: I would like to have an option in Miri to error when a int2ptr cast is executed.
2022-06-08 13:43:18 +02:00
bors
1a97162cb2 Auto merge of #94732 - nnethercote:infallible-encoder, r=bjorn3
Make `Encodable` and `Encoder` infallible.

A follow-up to #93066.

r? `@ghost`
2022-06-08 10:24:12 +00:00
lcnr
6ee7e35287 bye BorrowckMode 2022-06-08 10:46:52 +02:00
bors
e45d9973b2 Auto merge of #97860 - Dylan-DPC:rollup-t3vxos8, r=Dylan-DPC
Rollup of 5 pull requests

Successful merges:

 - #97595 (Remove unwrap from get_vtable)
 - #97597 (Preserve unused pointer to address casts)
 - #97819 (Recover `import` instead of `use` in item)
 - #97823 (Recover missing comma after match arm)
 - #97851 (Use repr(C) when depending on struct layout in ptr tests)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-06-08 08:05:47 +00:00
SparrowLii
8db6d4bae2 optimize superset method of IntervalSet 2022-06-08 15:23:11 +08:00
Chayim Refael Friedman
c1b1ec7e07 Suggest escaping box as identifier 2022-06-08 06:17:25 +00:00
Dylan DPC
a90c5a3c69
Rollup merge of #97823 - compiler-errors:missing-comma-match-arm, r=estebank
Recover missing comma after match arm

If we're missing a comma after a match arm expression, try parsing another pattern and a following `=>`. If we find both of those, then recover by suggesting to insert a `,`.

Fixes #80112
2022-06-08 07:37:32 +02:00
Dylan DPC
a64a9829c8
Rollup merge of #97819 - compiler-errors:use-import, r=wesleywiser
Recover `import` instead of `use` in item

When we definitely don't have a macro invocation (i.e. when we don't have `import ::`), then it's more productive to parse `import` as if it was incorrectly mistaken for `use`.

Not sure if this needs to be a verbose suggestion, but it renders strangely when it's not verbose:
```
error: expected item, found `import`
 --> /home/michael/test.rs:1:1
  |
1 | import std::{io::{self, Write}, rc::Rc};
  | ^^^^^^ help: items are imported using the `use` keyword: `use`
```

Happy to change it to `span_suggestion` instead of `span_suggestion_verbose` though.

Fixes #97788
2022-06-08 07:37:31 +02:00
Dylan DPC
d380b457d8
Rollup merge of #97597 - tmiasko:simplify-locals-side-effects, r=RalfJung,JakobDegen
Preserve unused pointer to address casts

Fixes #97421.

cc `@RalfJung`
2022-06-08 07:37:30 +02:00
Dylan DPC
148a44a001
Rollup merge of #97595 - ouz-a:issue-97381, r=compiler-errors
Remove unwrap from get_vtable

This avoids ICE on issue #97381 I think the bug is a bit deeper though, it compiles fine when `v` is `&v` which makes me think `Deref` is causing some issue with borrowck but it's fine I guess since this thing crashes since `nightly-2020-09-17` 😅
2022-06-08 07:37:29 +02:00
bors
64a7aa7016 Auto merge of #97447 - nnethercote:improve-folding, r=jackh726
Folding revamp

r? `@ghost`
2022-06-08 05:36:40 +00:00
Michael Goulet
2ae1ec9119 Don't suggest adding let in certain if conditions 2022-06-07 21:02:58 -07:00
Michael Goulet
b7ed860108 recover import instead of use in item 2022-06-07 19:14:03 -07:00
Caio
9edaa76adc Stabilize $$ in Rust 1.63.0 2022-06-07 21:50:45 -03:00
bors
47aee31b2a Auto merge of #97849 - matthiaskrgr:rollup-1yodhvw, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #97829 (Add regresion test for #95307)
 - #97831 (Remove `AlwaysLiveLocals` wrapper struct)
 - #97832 (Change `Direction::{is_forward,is_backward}` functions into constants)
 - #97840 (RustWrapper: adapt to APInt API changes in LLVM 15)
 - #97845 (Use more targeted suggestion when confusing i8 with std::i8)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-06-08 00:26:37 +00:00
Nicholas Nethercote
b983e42936 Rename rustc_serialize::opaque::Encoder as MemEncoder.
This avoids the name clash with `rustc_serialize::Encoder` (a trait),
and allows lots qualifiers to be removed and imports to be simplified
(e.g. fewer `as` imports).
2022-06-08 09:50:44 +10:00
Nicholas Nethercote
90db033955 Folding revamp.
This commit makes type folding more like the way chalk does it.

Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
  `super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
  calls into a `TypeFolder`, which can then call back into
  `super_fold_with`.

With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
  actual work of traversing a type, for all types except types of
  interest.
- `super_fold_with` is only implemented for the types of interest.

Benefits of the new model.
- I find it easier to understand. The distinction between types of
  interest and other types is clearer, and `super_fold_with` doesn't
  exist for most types.
- With the current model is easy to get confused and implement a
  `super_fold_with` method that should be left defaulted. (Some of the
  precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
  `TypeFolder` impls where `fold_with` should be called. The new
  approach makes this mistake impossible, and this commit fixes a number
  of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
  `super_fold_with` call in all cases except types of interest. A lot of
  the time the compile would inline those away, but not necessarily
  always.
2022-06-08 09:24:03 +10:00
Nicholas Nethercote
7480b501b4 Avoid some unnecessary returns. 2022-06-08 09:22:23 +10:00
Nicholas Nethercote
23880a058b Add try_fold_uenevaluted.
We already have `visit_unevaluated`, so this improves consistency.

Also, define `TypeFoldable for Unevaluated<'tcx, ()>` in terms of
`TypeFoldable for Unevaluated<'tcx>`, which is neater.
2022-06-08 09:22:23 +10:00
Nicholas Nethercote
6ba2dfd330 Add TypeVisitor::visit_mir_const.
Because `TypeFoldable::try_fold_mir_const` exists, and even though
`visit_mir_const` isn't needed right now, the consistency makes the code
easier to understand.
2022-06-08 09:22:23 +10:00
Nicholas Nethercote
ca7585ab9a Remove EarlyBinder::{try_fold_with,visit_with}.
For most types the default impls of these methods are good enough, and
`EarlyBinder` is one such type.
2022-06-08 09:22:23 +10:00
Nicholas Nethercote
28be201d2f Use super_visit_with in a couple of visit_binder methods.
Because it's equivalent but simpler to what's currently there.
2022-06-08 09:22:23 +10:00
Nicholas Nethercote
465d198c74 Rename TypeVisitor::visit_unevaluated_const.
To match the corresponding type name.
2022-06-08 09:22:23 +10:00
Nicholas Nethercote
dc08bc51f2 Move finish out of the Encoder trait.
This simplifies things, but requires making `CacheEncoder` non-generic.
2022-06-08 09:21:05 +10:00
Matthias Krüger
c2d84852e5
Rollup merge of #97845 - estebank:spancito, r=compiler-errors
Use more targeted suggestion when confusing i8 with std::i8

r? `@compiler-errors`
2022-06-07 23:55:29 +02:00
Matthias Krüger
5870156d85
Rollup merge of #97840 - durin42:llvm-15-apint, r=nikic
RustWrapper: adapt to APInt API changes in LLVM 15

In https://reviews.llvm.org/D125556 upstream changed sext() and zext()
to allow some no-op cases, which previously required use of the *OrSelf()
methods, which I assume is what was going on here. The *OrSelf() methods
got removed in https://reviews.llvm.org/D125559 after two weeks of
deprecation because they came with some bonus (probably-undesired)
behavior. Since the behavior of sext() and zext() changed slightly, I
kept the old *OrSelf() calls in LLVM 14 and earlier, and only use the
new version in LLVM 15.

r? `@nikic`
2022-06-07 23:55:28 +02:00
Matthias Krüger
796c466cea
Rollup merge of #97832 - tmiasko:const-direction, r=cjgillot
Change `Direction::{is_forward,is_backward}` functions into constants

Make it explicit that the analysis direction is constant.

This also makes the value immediately available for optimizations.
Previously those functions were neither inline nor generic and so their
definition was unavailable when using data flow framework from other
crates.
2022-06-07 23:55:27 +02:00
Matthias Krüger
c9cf8aeab8
Rollup merge of #97831 - tmiasko:rm-always-live-locals-struct, r=davidtwco
Remove `AlwaysLiveLocals` wrapper struct

It is just a wrapper around a `BitSet` and
doesn't have any functionality of its own.
2022-06-07 23:55:26 +02:00
bors
b17e9d76f2 Auto merge of #97081 - oli-obk:outlives_query_fast_path, r=jackh726
Re-use the type op instead of calling the implied_outlives_bounds query directly

r? `@ghost`
2022-06-07 21:44:40 +00:00
Santiago Pastorino
4ae69f8617
Extract lower_generic_and_bounds function 2022-06-07 18:29:07 -03:00
Santiago Pastorino
b239611451
Pass origin down to impl_trait_ty_to_ty 2022-06-07 18:29:07 -03:00
Santiago Pastorino
1e6ed67d37
Extract lower_generic_param_kind 2022-06-07 18:29:07 -03:00
Santiago Pastorino
a22aad32eb
Instrument important fns in AST lowering 2022-06-07 18:29:06 -03:00
Nicholas Nethercote
1acbe7573d Use delayed error handling for Encodable and Encoder infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.

Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).

This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.

This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.

Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
  `into_inner` method is changed into `finish`, which returns
  `Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
  strategy. Its `Ok` type is a `usize`, returning the number of bytes
  written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
  passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-08 07:01:26 +10:00