Commit graph

171 commits

Author SHA1 Message Date
Vadim Petrochenkov 29a9ef2818 expand: Align some code with the PR fixing inner attributes on out-of-line modules 2021-03-05 01:33:43 +03:00
Vadim Petrochenkov da3419e18c rustc_interface: Hide some hacky details of early linting from expand 2021-03-05 01:33:43 +03:00
Vadim Petrochenkov 46b67aa74d expand: Some more consistent naming in module loading 2021-03-05 01:33:43 +03:00
Vadim Petrochenkov 3d0b622ab7 expand: Less path cloning during module loading 2021-03-05 01:33:43 +03:00
Vadim Petrochenkov 5bdf81d5fa expand: Determine module directory path directly instead of relying on span 2021-03-05 01:33:43 +03:00
Vadim Petrochenkov 39052c55bb expand: Move module file path stack from global session to expansion data
Also don't push the paths on the stack directly in `fn parse_external_mod`, return them instead.
2021-03-05 01:33:43 +03:00
Vadim Petrochenkov bc18eb4717 expand: Remove obsolete DirectoryOwnership::UnownedViaMod
This ownership kind is only constructed in the case of path attributes like `#[path = ".."]` without a file name segment, which always represent some kind of directories and will produce and error on attempt to parse them as a module file.
2021-03-05 01:33:43 +03:00
Aaron Hill fb5fec017b
Combine HasAttrs and HasTokens into AstLike
When token-based attribute handling is implemeneted in #80689,
we will need to access tokens from `HasAttrs` (to perform
cfg-stripping), and we will to access attributes from `HasTokens` (to
construct a `PreexpTokenStream`).

This PR merges the `HasAttrs` and `HasTokens` traits into a new
`AstLike` trait. The previous `HasAttrs` impls from `Vec<Attribute>` and `AttrVec`
are removed - they aren't attribute targets, so the impls never really
made sense.
2021-02-27 00:14:13 -05:00
Vadim Petrochenkov ddd20ef8cb expand: Do not allocate Lrc for allow_internal_unstable list unless necessary 2021-02-23 01:50:34 +03:00
Vadim Petrochenkov fc9d578bc5 expand: Preserve order of inert attributes during expansion 2021-02-23 01:07:22 +03:00
Matthias Krüger da9a588d4f remove redundant wrapping of return types of allow_internal_unstable() and rustc_allow_const_fn_unstable() 2021-02-21 18:11:27 +01:00
Vadim Petrochenkov 4a88165124 ast: Keep expansion status for out-of-line module items
Also remove `ast::Mod` which is mostly redundant now
2021-02-18 13:07:49 +03:00
Vadim Petrochenkov eb65f15c78 ast: Stop using Mod in Crate
Crate root is sufficiently different from `mod` items, at least at syntactic level.

Also remove customization point for "`mod` item or crate root" from AST visitors.
2021-02-18 13:07:49 +03:00
Dylan DPC 91e5384fc0
Rollup merge of #81869 - mark-i-m:leading-vert, r=petrochenkov
Simplify pattern grammar, improve or-pattern diagnostics

This implements the change under FCP in https://github.com/rust-lang/rust/issues/81415. It allows nested or-patterns to contain a leading `|`, simplifying the [grammar for patterns](https://github.com/rust-lang/reference/pull/957/files?short_path=cc629f1#diff-cc629f15712821139bc706c63b3845ab59a008e2a998e08ffad42e3aebcbcbe2).

Along the way, we also improve the diagnostics around a few specially-handled cases, such as using `||` instead of `|`, using or-patterns in fn params, including the leading `|` in the pattern span, etc.

r? `@petrochenkov`
2021-02-17 23:51:16 +01:00
mark aee1e59e6f Simplify pattern grammar by allowing nested leading vert
Along the way, we also implement a handful of diagnostics improvements
and fixes, particularly with respect to the special handling of `||` in
place of `|` and when there are leading verts in function params, which
don't allow top-level or-patterns anyway.
2021-02-15 12:07:54 -06:00
Jonas Schievink 1c75dfbce7
Rollup merge of #82129 - est31:master, r=jyn514
Remove redundant bool_to_option feature gate
2021-02-15 16:07:09 +01:00
Jonas Schievink 285ea2f80d
Rollup merge of #82107 - petrochenkov:minexpclean, r=Aaron1011
expand: Some cleanup

See individual commits for details.

r? ``@Aaron1011``
2021-02-15 16:07:04 +01:00
est31 63806cc919 Remove redundant bool_to_option feature gate 2021-02-15 04:27:57 +01:00
Vadim Petrochenkov 6e11a8b66a expand: Remove redundant calls to configure
Starting from https://github.com/rust-lang/rust/pull/63468 cfg attributes on variants, fields, fn params etc. are processed together with other attributes (via `configure!`).
2021-02-14 19:47:00 +03:00
Vadim Petrochenkov 18c94b3edd expand: Remove obsolete ExpansionConfig::keep_macs
Maybe it was used before the introduction of placeholders, but now it has no effect.
2021-02-14 19:43:54 +03:00
klensy 93c8ebe022 bumped smallvec deps 2021-02-14 18:03:11 +03:00
Esteban Küber 49310cee30 Add test for "const stability on macro" 2021-02-11 11:30:05 -08:00
Esteban Küber 19806e4514 Tweak stability attribute diagnostic output 2021-02-10 21:35:27 -08:00
Skgland e1010424dc
add method to construct def site path as a vec of idents
like std_path but used dummy span for all path elements and does not perpend kw:DollarCrate
2021-02-09 13:42:35 +01:00
Vadim Petrochenkov d8af6de911 Address review comments 2021-02-07 20:08:45 +03:00
Vadim Petrochenkov dbdbd30bf2 expand/resolve: Turn #[derive] into a regular macro attribute 2021-02-07 20:08:45 +03:00
Aaron Hill 6c14aad58e
Improve handling of spans around macro result parse errors
Fixes #81543

After we expand a macro, we try to parse the resulting tokens as a AST
node. This commit makes several improvements to how we handle spans when
an error occurs:

* Only ovewrite the original `Span` if it's a dummy span. This preserves
  a more-specific span if one is available.
* Use `self.prev_token` instead of `self.token` when emitting an error
  message after encountering EOF, since an EOF token always has a dummy
  span
* Make `SourceMap::next_point` leave dummy spans unused. A dummy span
  does not have a logical 'next point', since it's a zero-length span.
  Re-using the span span preserves its 'dummy-ness' for other checks
2021-01-31 15:24:34 -05:00
Aaron Hill f9025512e7
Add SEMICOLON_IN_EXPRESSIONS_FROM_MACROS lint
cc #79813

This PR adds an allow-by-default future-compatibility lint
`SEMICOLON_IN_EXPRESSIONS_FROM_MACROS`. It fires when a trailing semicolon in a
macro body is ignored due to the macro being used in expression
position:

```rust
macro_rules! foo {
    () => {
        true; // WARN
    }
}

fn main() {
    let val = match true {
        true => false,
        _ => foo!()
    };
}
```

The lint takes its level from the macro call site, and
can be allowed for a particular macro by adding
`#[allow(semicolon_in_expressions_from_macros)]`.

The lint is set to warn for all internal rustc crates (when being built
by a stage1 compiler). After the next beta bump, we can enable
the lint for the bootstrap compiler as well.
2021-01-28 08:51:43 -05:00
Yuki Okushi bb6d1d3086
Rollup merge of #81284 - jyn514:impl-times, r=wesleywiser
Make `-Z time-passes` less noisy

- Add the module name to `pre_AST_expansion_passes` and don't make it a
  verbose event (since it normally doesn't take very long, and it's
  emitted many times)
- Don't make the following rustdoc events verbose; they're emitted many times.
  + build_extern_trait_impl
  + build_local_trait_impl
  + build_primitive_trait_impl
  + get_auto_trait_impls
  + get_blanket_trait_impls
- Remove the `get_auto_trait_and_blanket_synthetic_impls` rustdoc event; it's wholly
  covered by get_{auto,blanket}_trait_impls and not very useful.

I found this while working on https://github.com/rust-lang/rust/pull/81275 but it's independent of those changes.
2021-01-28 15:09:10 +09:00
Joshua Nelson 3b8f1b7883 Make -Z time-passes less noisy
- Add the module name to `pre_AST_expansion_passes` and don't make it a
  verbose event (since it normally doesn't take very long, and it's
  emitted many times)
- Don't make the following rustdoc events verbose; they're emitted many times.
  + build_extern_trait_impl
  + build_local_trait_impl
  + build_primitive_trait_impl
  + get_auto_trait_impls
  + get_blanket_trait_impls
- Remove `get_auto_trait_and_blanket_synthetic_impls`; it's wholly
  covered by get_{auto,blanket}_trait_impls and not very useful.
2021-01-23 11:44:46 -05:00
Aaron Hill 11b1e37016
Force token collection to run when parsing nonterminals
Fixes #81007

Previously, we would fail to collect tokens in the proper place when
only builtin attributes were present. As a result, we would end up with
attribute tokens in the collected `TokenStream`, leading to duplication
when we attempted to prepend the attributes from the AST node.

We now explicitly track when token collection must be performed due to
nomterminal parsing.
2021-01-20 18:09:32 -05:00
Ryan Levick f07dd6d41a Remove dead code 2021-01-17 12:45:48 +01:00
LingMan a56bffb4f9 Use Option::map_or instead of .map(..).unwrap_or(..) 2021-01-14 19:23:59 +01:00
bors fc9944fe84 Auto merge of #80499 - matthiaskrgr:red_clos, r=estebank
remove redundant closures (clippy::redundant_closure)
2021-01-12 11:20:47 +00:00
Vadim Petrochenkov f9b5859173 resolve: Simplify built-in macro table 2021-01-10 14:48:47 +03:00
Yuki Okushi 3e735c6e93
Rollup merge of #80850 - m-ou-se:rustc-builtin-macro-name, r=petrochenkov
Allow #[rustc_builtin_macro = "name"]

This adds the option of specifying the name of a builtin macro in the `#[rustc_builtin_macro]` attribute: `#[rustc_builtin_macro = "name"]`.

This makes it possible to have both `std::panic!` and `core::panic!` as a builtin macro, by using different builtin macro names for each. This is needed to implement the edition-specific behaviour of the panic macros of RFC 3007.

Also removes `SyntaxExtension::is_derive_copy`, as the macro name (e.g. `sym::Copy`) is now tracked and provides that information directly.

r? ``@petrochenkov``
2021-01-10 16:56:05 +09:00
Mara Bos 0aad91b990 Formatting. 2021-01-09 19:50:12 +01:00
Mara Bos d651fa78ce Allow #[rustc_builtin_macro = "name"].
This makes it possible to have both std::panic and core::panic as a
builtin macro, by using different builtin macro names for each.

Also removes SyntaxExtension::is_derive_copy, as the macro name (e.g.
sym::Copy) is now tracked and provides that information directly.
2021-01-09 19:50:06 +01:00
Vadim Petrochenkov d81c1946c6 resolve/expand: Improve attribute expansion on macro definitions and calls 2021-01-09 18:43:01 +03:00
Vadim Petrochenkov 0dab076358 rustc_parse: Better spans for synthesized token streams 2021-01-07 17:48:13 +03:00
Aaron Hill 21b8f2ecde
Make ExpnData fields krate and orig_id private
These fields are only used by hygiene serialized, and should not be
accessed by anything outside of `rustc_span`.
2021-01-03 08:58:43 -05:00
Matthias Krüger e2272cdffc remove redundant closures (clippy::redundant_closure) 2021-01-03 13:34:24 +01:00
bors 44e3daf5ee Auto merge of #80459 - mark-i-m:or-pat-reg, r=petrochenkov
Implement edition-based macro :pat feature

This PR does two things:
1. Fixes the perf regression from https://github.com/rust-lang/rust/pull/80100#issuecomment-750893149
2. Implements `:pat2018` and `:pat2021` matchers, as described by `@joshtriplett`  in https://github.com/rust-lang/rust/issues/54883#issuecomment-745509090 behind the feature gate `edition_macro_pat`.

r? `@petrochenkov`

cc `@Mark-Simulacrum`
2020-12-31 14:52:26 +00:00
mark 40bf3c0f09 Implement edition-based macro pat feature 2020-12-30 09:57:49 -06:00
Aaron Hill 530a629635
Remove pretty-print/reparse hack, and add derive-specific hack 2020-12-29 09:36:42 -05:00
Wesley Wiser f1eb88b28a Revert "Promote missing_fragment_specifier to hard error"
This reverts commit 02eae432e7.
2020-12-22 09:33:16 -05:00
mark 1a7d00a529 implement edition-specific :pat behavior for 2015/18 2020-12-19 07:13:36 -06:00
Camelid bec1c278b6
Remove docs for non-existent parameters in rustc_expand 2020-12-16 17:34:47 -08:00
Vadim Petrochenkov 05b557cfc9 Remove some no longer necessary #[cfg(test)]s
With https://github.com/rust-lang/rust/pull/69838 inner modules are never touched in the outer module is unconfigured.
2020-12-12 19:20:37 +03:00
Dylan DPC 5cebbaa6a1
Rollup merge of #79678 - jyn514:THE-PAPERCLIP-COMETH, r=varkor
Fix some clippy lints

Happy to revert these if you think they're less readable, but personally I like them better now (especially the `else { if { ... } }` to `else if { ... }` change).
2020-12-04 03:30:39 +01:00
Joshua Nelson 0ad3dce83a Fix some clippy lints 2020-12-03 17:08:19 -05:00
Guillaume Gomez 7df0052df8 Created NestedMetaItem::name_value_literal_span method 2020-12-01 16:26:51 +01:00
Aaron Hill de88bf148b
Properly handle attributes on statements
We now collect tokens for the underlying node wrapped by `StmtKind`
instead of storing tokens directly in `Stmt`.

`LazyTokenStream` now supports capturing a trailing semicolon after it
is initially constructed. This allows us to avoid refactoring statement
parsing to wrap the parsing of the semicolon in `parse_tokens`.

Attributes on item statements
(e.g. `fn foo() { #[bar] struct MyStruct; }`) are now treated as
item attributes, not statement attributes, which is consistent with how
we handle attributes on other kinds of statements. The feature-gating
code is adjusted so that proc-macro attributes are still allowed on item
statements on stable.

Two built-in macros (`#[global_allocator]` and `#[test]`) needed to be
adjusted to support being passed `Annotatable::Stmt`.
2020-11-26 17:08:35 -05:00
Aaron Hill 9c9f40656d
Invoke attributes on the statement for statement items 2020-11-24 16:38:58 -05:00
bors 74285eb3a8 Auto merge of #78088 - fusion-engineering-forks:panic-fmt-lint, r=estebank
Add lint for panic!("{}")

This adds a lint that warns about `panic!("{}")`.

`panic!(msg)` invocations with a single argument use their argument as panic payload literally, without using it as a format string. The same holds for `assert!(expr, msg)`.

This lints checks if `msg` is a string literal (after expansion), and warns in case it contained braces. It suggests to insert `"{}", ` to use the message literally, or to add arguments to use it as a format string.

![image](https://user-images.githubusercontent.com/783247/96643867-79eb1080-1328-11eb-8d4e-a5586837c70a.png)

This lint is also a good starting point for adding warnings about `panic!(not_a_string)` later, once [`panic_any()`](https://github.com/rust-lang/rust/pull/74622) becomes a stable alternative.
2020-11-20 03:40:20 +00:00
Vadim Petrochenkov d575aa4d58 expand: Mark some dead code in derive expansion as unreachable 2020-11-19 19:25:20 +03:00
Vadim Petrochenkov cd2177f3de expand: Stop derive expansion un unexpected targets early
Collect derive placeholders using `collect` instead of `push`
2020-11-19 19:25:20 +03:00
Vadim Petrochenkov ec547202b4 expand: Cleanup attribute collection in invocation collector 2020-11-19 19:25:20 +03:00
Vadim Petrochenkov dfb690eaa9 resolve/expand: Misc cleanup 2020-11-19 19:25:20 +03:00
Vadim Petrochenkov 68f94e94ed resolve: Centralize some error reporting for unexpected macro resolutions 2020-11-19 19:25:20 +03:00
Vadim Petrochenkov e7ee4d66ce expand: Move fully_configure to config.rs 2020-11-19 19:25:20 +03:00
Vadim Petrochenkov b49fbc9432 expand: Tell built-in macros whether we are currently in forced expansion mode 2020-11-19 19:25:20 +03:00
Mara Bos 755dd14e00
Rollup merge of #78836 - fanzier:struct-and-slice-destructuring, r=petrochenkov
Implement destructuring assignment for structs and slices

This is the second step towards implementing destructuring assignment (RFC: rust-lang/rfcs#2909, tracking issue: #71126). This PR is the second part of #71156, which was split up to allow for easier review.

Note that the first PR (#78748) is not merged yet, so it is included as the first commit in this one. I thought this would allow the review to start earlier because I have some time this weekend to respond to reviews. If ``@petrochenkov`` prefers to wait until the first PR is merged, I totally understand, of course.

This PR implements destructuring assignment for (tuple) structs and slices. In order to do this, the following *parser change* was necessary: struct expressions are not required to have a base expression, i.e. `Struct { a: 1, .. }` becomes legal (in order to act like a struct pattern).

Unfortunately, this PR slightly regresses the diagnostics implemented in #77283. However, it is only a missing help message in `src/test/ui/issues/issue-77218.rs`. Other instances of this diagnostic are not affected. Since I don't exactly understand how this help message works and how to fix it yet, I was hoping it's OK to regress this temporarily and fix it in a follow-up PR.

Thanks to ``@varkor`` who helped with the implementation, particularly around the struct rest changes.

r? ``@petrochenkov``
2020-11-12 19:46:09 +01:00
bors 5a6a41e784 Auto merge of #78782 - petrochenkov:nodoctok, r=Aaron1011
Do not collect tokens for doc comments

Doc comment is a single token and AST has all the information to re-create it precisely.
Doc comments are also responsible for majority of calls to `collect_tokens` (with `num_calls == 1` and `num_calls == 0`, cc https://github.com/rust-lang/rust/pull/78736).

(I also moved token collection into `fn parse_attribute` to deduplicate code a bit.)

r? `@Aaron1011`
2020-11-12 00:33:55 +00:00
Fabian Zaiser de84ad95b4 Implement destructuring assignment for structs and slices
Co-authored-by: varkor <github@varkor.com>
2020-11-11 12:10:52 +00:00
Dylan DPC 8ebca242bc
Rollup merge of #78710 - petrochenkov:macvisit, r=davidtwco
rustc_ast: Do not panic by default when visiting macro calls

Panicking by default made sense when we didn't have HIR or MIR and everything worked on AST, but now all AST visitors run early and majority of them have to deal with macro calls, often by ignoring them.

The second commit renames `visit_mac` to `visit_mac_call`, the corresponding structures were renamed earlier in https://github.com/rust-lang/rust/pull/69589.
2020-11-09 19:06:55 +01:00
Vadim Petrochenkov 12de1e8985 Do not collect tokens for doc comments 2020-11-09 01:47:11 +03:00
o752d 2550e887b3
typo and formatting
fixed a typo and modified some line formatting justification while I'm here :)
2020-11-07 23:25:10 +00:00
est31 de2940ff63 rustc_expand: use collect_bang helper instead of manual reimplementation 2020-11-06 03:16:56 +01:00
Vadim Petrochenkov 90fafc8c8f rustc_ast: visit_mac -> visit_mac_call 2020-11-03 23:39:51 +03:00
Mara Bos f0112928cb
Rollup merge of #78626 - fusion-engineering-forks:deprecated-trait-impl, r=estebank
Improve errors about #[deprecated] attribute

This change:

1. Turns `#[deprecated]` on a trait impl block into an error, which fixes #78625;
2. Changes these and other errors about `#[deprecated]` to use the span of the attribute instead of the item; and
3. Turns this error into a lint, to make sure it can be capped with `--cap-lints` and doesn't break any existing dependencies.

Can be reviewed per commit.

---
Example:
```rust
struct X;

#[deprecated = "a"]
impl Default for X {
    #[deprecated = "b"]
    fn default() -> Self {
        X
    }
}
```

Before:
```
error: This deprecation annotation is useless
 --> src/main.rs:6:5
  |
6 | /     fn default() -> Self {
7 | |         X
8 | |     }
  | |_____^
```

After:
```
error: this `#[deprecated]' annotation has no effect
 --> src/main.rs:3:1
  |
3 | #[deprecated = "a"]
  | ^^^^^^^^^^^^^^^^^^^ help: try removing the deprecation attribute
  |
  = note: `#[deny(useless_deprecated)]` on by default

error: this `#[deprecated]' annotation has no effect
 --> src/main.rs:5:5
  |
5 |     #[deprecated = "b"]
  |     ^^^^^^^^^^^^^^^^^^^ help: try removing the deprecation attribute
```
2020-11-03 19:32:36 +01:00
Vadim Petrochenkov 3237b3886c rustc_ast: Do not panic by default when visiting macro calls 2020-11-03 20:38:20 +03:00
Yuki Okushi 0716724a0b
Rollup merge of #78376 - Aaron1011:feature/consistent-empty-expr, r=petrochenkov
Treat trailing semicolon as a statement in macro call

See #61733 (comment)

We now preserve the trailing semicolon in a macro invocation, even if
the macro expands to nothing. As a result, the following code no longer
compiles:

```rust
macro_rules! empty {
    () => { }
}

fn foo() -> bool { //~ ERROR mismatched
    { true } //~ ERROR mismatched
    empty!();
}
```

Previously, `{ true }` would be considered the trailing expression, even
though there's a semicolon in `empty!();`

This makes macro expansion more token-based.
2020-11-03 15:27:03 +09:00
Vadim Petrochenkov 19dbb02a89 Expand NtExpr tokens only in key-value attributes 2020-11-03 00:53:43 +03:00
Aaron Hill e78e9d4a06
Treat trailing semicolon as a statement in macro call
See https://github.com/rust-lang/rust/issues/61733#issuecomment-716188981

We now preserve the trailing semicolon in a macro invocation, even if
the macro expands to nothing. As a result, the following code no longer
compiles:

```rust
macro_rules! empty {
    () => { }
}

fn foo() -> bool { //~ ERROR mismatched
    { true } //~ ERROR mismatched
    empty!();
}
```

Previously, `{ true }` would be considered the trailing expression, even
though there's a semicolon in `empty!();`

This makes macro expansion more token-based.
2020-11-02 13:03:13 -05:00
Mara Bos 706bc33651 Use the right span for errors about #[deprecated] attributes. 2020-11-01 20:48:58 +01:00
Mara Bos 540d4743cc
Rollup merge of #78603 - petrochenkov:fourdigits, r=matthewjasper
expand: Tweak a comment in implementation of `macro_rules`

The answer to the removed FIXME is that we don't apply mark to the span `sp` just because that span is no longer used. We could apply it, but that would just be unnecessary extra work.

The comments in code tell why the span is unused, it's a span of `$var` literally, which is lost for `tt` variables because their tokens are outputted directly, but kept for other variables which are outputted as [groups](https://doc.rust-lang.org/nightly/proc_macro/struct.Group.html) and `sp` is kept as the group's span.

Closes https://github.com/rust-lang/rust/issues/2887
2020-11-01 11:53:37 +01:00
Vadim Petrochenkov 7652bc3f68 expand: Tweak a comment in implementation of macro_rules 2020-10-31 18:13:46 +03:00
Vadim Petrochenkov d0c63bccc5 parser: Cleanup LazyTokenStream and avoid some clones
by using a named struct instead of a closure.
2020-10-31 01:56:34 +03:00
Joshua Nelson bfecb18771 Fix some more clippy warnings 2020-10-30 10:12:56 -04:00
Yuki Okushi 0a26e4ba7e
Rollup merge of #78326 - Aaron1011:fix/min-stmt-lints, r=petrochenkov
Split out statement attributes changes from #78306

This is the same as PR https://github.com/rust-lang/rust/pull/78306, but `unused_doc_comments` is modified to explicitly ignore statement items (which preserves the current behavior).

This shouldn't have any user-visible effects, so it can be landed without lang team discussion.

---------
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.

```rust
trait Foo {
    #[allow(unused_attributes)] #[inline] fn first();
    #[inline] #[allow(unused_attributes)] fn second();
}
```

However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).

Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.

This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:

* The `HasAttr` impl for `StmtKind` now returns attributes for
  `StmtKind::Item`, treating it just like every other `StmtKind`
  variant. The only place relying on the old behavior was macro
  which has been updated to explicitly ignore attributes on item
  statements. This allows the `unused_doc_comments` lint to fire for
  item statements.
* The `early` and `late` lint visitors now activate lint attributes when
  invoking the callback for `Stmt`. This ensures that a lint
  attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
  sibiling attributes on an item statement.

For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-25 18:43:49 +09:00
Aaron Hill ac384ac2db
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.

```rust
trait Foo {
    #[allow(unused_attributes)] #[inline] fn first();
    #[inline] #[allow(unused_attributes)] fn second();
}
```

However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).

Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.

This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:

* The `HasAttr` impl for `StmtKind` now returns attributes for
  `StmtKind::Item`, treating it just like every other `StmtKind`
  variant. The only place relying on the old behavior was macro
  which has been updated to explicitly ignore attributes on item
  statements. This allows the `unused_doc_comments` lint to fire for
  item statements.
* The `early` and `late` lint visitors now activate lint attributes when
  invoking the callback for `Stmt`. This ensures that a lint
  attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
  sibiling attributes on an item statement.

For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-24 11:55:48 -04:00
Aaron Hill b9b2546417
Unconditionally capture tokens for attributes.
This allows us to avoid synthesizing tokens in `prepend_attr`, since we
have the original tokens available.

We still need to synthesize tokens when expanding `cfg_attr`,
but this is an unavoidable consequence of the syntax of `cfg_attr` -
the user does not supply the `#` and `[]` tokens that a `cfg_attr`
expands to.
2020-10-21 18:57:29 -04:00
Aaron Hill f6aec82d4d
Avoid cloning the contents of a TokenStream in a few places 2020-10-19 12:30:41 -04:00
Mara Bos 9a840a30d6 Fix brace problem in panic message in rustc_expand. 2020-10-19 00:05:45 +02:00
est31 215cd36e1c Remove unused code from remaining compiler crates 2020-10-14 04:14:32 +02:00
Esteban Küber e5f83bcd04 Detect blocks that could be struct expr bodies
This approach lives exclusively in the parser, so struct expr bodies
that are syntactically correct on their own but are otherwise incorrect
will still emit confusing errors, like in the following case:

```rust
fn foo() -> Foo {
    bar: Vec::new()
}
```

```
error[E0425]: cannot find value `bar` in this scope
 --> src/file.rs:5:5
  |
5 |     bar: Vec::new()
  |     ^^^ expecting a type here because of type ascription

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/file.rs:5:15
  |
5 |     bar: Vec::new()
  |               ^^^^^ only `Fn` traits may use parentheses

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/file.rs:5:10
  |
5 |     bar: Vec::new()
  |          ^^^^^^^^^^ expected 1 type argument
  ```

If that field had a trailing comma, that would be a parse error and it
would trigger the new, more targetted, error:

```
error: struct literal body without path
 --> file.rs:4:17
  |
4 |   fn foo() -> Foo {
  |  _________________^
5 | |     bar: Vec::new(),
6 | | }
  | |_^
  |
help: you might have forgotten to add the struct literal inside the block
  |
4 | fn foo() -> Foo { Path {
5 |     bar: Vec::new(),
6 | } }
  |
```

Partially address last part of #34255.
2020-10-07 13:40:52 -07:00
Guillaume Gomez d1d94ba026 Improve E0777 help message 2020-10-02 16:33:44 +02:00
Guillaume Gomez 8b7aeefede Import struct_span_err macro instead of prepending it 2020-10-01 20:41:57 +02:00
Guillaume Gomez de21c3df0e Create E0777 error code for "invalid literal in derive" 2020-10-01 20:41:57 +02:00
Vadim Petrochenkov 4a4a7f8599 expand: Minor fn ptr call cleanup 2020-09-28 23:10:48 +03:00
Vadim Petrochenkov 85ef265dbe expand: Stop un-interpolating NtIdents before passing them to built-in macros
This was a big hack, and built-in macros should be able to deal with `NtIdents` in the input by themselves like any other parser code.
2020-09-28 23:10:44 +03:00
est31 ebdea01143 Remove redundant #![feature(...)] 's from compiler/ 2020-09-17 07:58:45 +02:00
Dylan DPC fb9bb2b5ca
Rollup merge of #75146 - tmiasko:range-overflow, r=Mark-Simulacrum
Detect overflow in proc_macro_server subspan

* Detect overflow in proc_macro_server subspan
* Add tests for overflow in Vec::drain
* Add tests for overflow in String / VecDeque operations using ranges
2020-09-16 01:30:30 +02:00
Aaron Hill 156ef2bee8
Attach tokens to ast::Stmt
We currently only attach tokens when parsing a `:stmt` matcher for a
`macro_rules!` macro. Proc-macro attributes on statements are still
unstable, and need additional work.
2020-09-10 17:33:06 -04:00
Aaron Hill c1011165e6
Attach TokenStream to ast::Visibility
A `Visibility` does not have outer attributes, so we only capture tokens
when parsing a `macro_rules!` matcher
2020-09-10 17:33:06 -04:00
Aaron Hill 55082ce413
Attach TokenStream to ast::Path 2020-09-10 17:33:06 -04:00
Aaron Hill 3815e91ccd
Attach tokens to NtMeta (ast::AttrItem)
An `AttrItem` does not have outer attributes, so we only capture tokens
when parsing a `macro_rules!` matcher
2020-09-10 17:33:06 -04:00
Aaron Hill 1823dea7df
Attach TokenStream to ast::Ty
A `Ty` does not have outer attributes, so we only capture tokens
when parsing a `macro_rules!` matcher
2020-09-10 17:33:05 -04:00
Aaron Hill de4bd9f0f8
Attach TokenStream to ast::Block
A `Block` does not have outer attributes, so we only capture tokens when
parsing a `macro_rules!` matcher
2020-09-10 17:33:05 -04:00