Skip to content

Rollup of 11 pull requests #79214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6554086
Add u128 and i128 integer tests
CDirkx Nov 14, 2020
69477f5
Clarify availability of atomic operations
Nov 17, 2020
b491587
Extract write_srclink to its own method
aDotInTheVoid Nov 18, 2020
1094f97
Test drop order for (destructuring) assignments
fanzier Nov 18, 2020
ae644a2
add [src] links to methods on a trait's page
aDotInTheVoid Nov 18, 2020
c711833
Qualify `panic!` as `core::panic!` in non-built-in `core` macros
camelid Oct 25, 2020
50b34c4
Clean up `core` macros documentation
camelid Oct 25, 2020
c48ed78
Add two more test cases
camelid Oct 29, 2020
a050c55
Remove unused `use std::panic;`s
camelid Oct 30, 2020
566e877
Make compiletest testing use the local sysroot
cuviper Nov 18, 2020
8afa22d
Never inline naked functions
tmiasko Nov 19, 2020
8247223
Revert "Always use param_env_reveal_all_normalized in validator"
tmiasko Nov 19, 2020
0ab4458
Revert "Normalize function type during validation"
tmiasko Nov 19, 2020
de08df2
Make as{_mut,}_slice on array::IntoIter public
est31 Nov 19, 2020
0510fd3
Update 32 bit mir-opt test output.
m-ou-se Nov 19, 2020
54588c8
Add jyn514 email alias to mailmap
pickfire Nov 19, 2020
b49fbc9
expand: Tell built-in macros whether we are currently in forced expan…
petrochenkov Nov 14, 2020
e7ee4d6
expand: Move `fully_configure` to `config.rs`
petrochenkov Nov 18, 2020
69894ce
resolve: Introduce a separate `NonMacroAttrKind` for legacy derive he…
petrochenkov Nov 18, 2020
68f94e9
resolve: Centralize some error reporting for unexpected macro resolut…
petrochenkov Nov 18, 2020
dfb690e
resolve/expand: Misc cleanup
petrochenkov Nov 18, 2020
ec54720
expand: Cleanup attribute collection in invocation collector
petrochenkov Nov 18, 2020
cd2177f
expand: Stop derive expansion un unexpected targets early
petrochenkov Nov 18, 2020
d575aa4
expand: Mark some dead code in derive expansion as unreachable
petrochenkov Nov 18, 2020
78c16cf
Rollup merge of #78343 - camelid:macros-qualify-panic, r=m-ou-se
Dylan-DPC Nov 19, 2020
3778485
Rollup merge of #79119 - jamesmunns:patch-1, r=Mark-Simulacrum
Dylan-DPC Nov 19, 2020
5a23e4a
Rollup merge of #79123 - CDirkx:128-bits, r=Mark-Simulacrum
Dylan-DPC Nov 19, 2020
34c9af1
Rollup merge of #79177 - fanzier:drop-order-test, r=RalfJung
Dylan-DPC Nov 19, 2020
ab648d1
Rollup merge of #79181 - aDotInTheVoid:provided-method-source-link, r…
Dylan-DPC Nov 19, 2020
7def739
Rollup merge of #79183 - cuviper:compiletest-test-sysroot, r=Mark-Sim…
Dylan-DPC Nov 19, 2020
cf82316
Rollup merge of #79185 - petrochenkov:derattr2, r=Aaron1011
Dylan-DPC Nov 19, 2020
b152b83
Rollup merge of #79192 - tmiasko:naked-noinline, r=oli-obk
Dylan-DPC Nov 19, 2020
cdff7bd
Rollup merge of #79193 - tmiasko:revert-78969-normalize, r=davidtwco
Dylan-DPC Nov 19, 2020
7acbc89
Rollup merge of #79194 - est31:array_into_iter_slice, r=scottmcm
Dylan-DPC Nov 19, 2020
822b420
Rollup merge of #79204 - pickfire:patch-3, r=jyn514
Dylan-DPC Nov 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolve: Centralize some error reporting for unexpected macro resolut…
…ions
  • Loading branch information
petrochenkov committed Nov 19, 2020
commit 68f94e94ed3d80d768d0d107049f02fb99716dbe
41 changes: 24 additions & 17 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,28 @@ impl AstFragmentKind {
self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
}

/// Fragment supports macro expansion and not just inert attributes, `cfg` and `cfg_attr`.
pub fn supports_macro_expansion(self) -> bool {
match self {
AstFragmentKind::OptExpr
| AstFragmentKind::Expr
| AstFragmentKind::Pat
| AstFragmentKind::Ty
| AstFragmentKind::Stmts
| AstFragmentKind::Items
| AstFragmentKind::TraitItems
| AstFragmentKind::ImplItems
| AstFragmentKind::ForeignItems => true,
AstFragmentKind::Arms
| AstFragmentKind::Fields
| AstFragmentKind::FieldPats
| AstFragmentKind::GenericParams
| AstFragmentKind::Params
| AstFragmentKind::StructFields
| AstFragmentKind::Variants => false,
}
}

fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(
self,
items: I,
Expand Down Expand Up @@ -1014,30 +1036,15 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
attrs: &mut Vec<ast::Attribute>,
after_derive: &mut bool,
) -> Option<ast::Attribute> {
let attr = attrs
attrs
.iter()
.position(|a| {
if a.has_name(sym::derive) {
*after_derive = true;
}
!self.cx.sess.is_attr_known(a) && !is_builtin_attr(a)
})
.map(|i| attrs.remove(i));
if let Some(attr) = &attr {
if !self.cx.ecfg.custom_inner_attributes()
&& attr.style == ast::AttrStyle::Inner
&& !attr.has_name(sym::test)
{
feature_err(
&self.cx.sess.parse_sess,
sym::custom_inner_attributes,
attr.span,
"non-builtin inner attributes are unstable",
)
.emit();
}
}
attr
.map(|i| attrs.remove(i))
}

/// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
Expand Down
98 changes: 58 additions & 40 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ use rustc_ast_pretty::pprust;
use rustc_attr::StabilityLevel;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::ptr_key::PtrKey;
use rustc_data_structures::sync::Lrc;
use rustc_errors::struct_span_err;
use rustc_expand::base::{Indeterminate, InvocationRes, ResolverExpand, SyntaxExtension};
use rustc_expand::compile_declarative_macro;
use rustc_expand::expand::{AstFragment, AstFragmentKind, Invocation, InvocationKind};
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind};
use rustc_feature::is_builtin_attr_name;
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
use rustc_hir::def_id;
use rustc_middle::middle::stability;
use rustc_middle::ty;
use rustc_session::lint::builtin::UNUSED_MACROS;
use rustc_session::parse::feature_err;
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::hygiene::{self, ExpnData, ExpnId, ExpnKind};
use rustc_span::hygiene::{AstPass, MacroKind};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};

use rustc_data_structures::sync::Lrc;
use rustc_span::hygiene::{AstPass, MacroKind};
use std::cell::Cell;
use std::{mem, ptr};

Expand Down Expand Up @@ -241,15 +241,20 @@ impl<'a> ResolverExpand for Resolver<'a> {
}
};

let (path, kind, derives, after_derive) = match invoc.kind {
let (path, kind, inner_attr, derives, after_derive) = match invoc.kind {
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } => (
&attr.get_normal_item().path,
MacroKind::Attr,
attr.style == ast::AttrStyle::Inner,
self.arenas.alloc_ast_paths(derives),
after_derive,
),
InvocationKind::Bang { ref mac, .. } => (&mac.path, MacroKind::Bang, &[][..], false),
InvocationKind::Derive { ref path, .. } => (path, MacroKind::Derive, &[][..], false),
InvocationKind::Bang { ref mac, .. } => {
(&mac.path, MacroKind::Bang, false, &[][..], false)
}
InvocationKind::Derive { ref path, .. } => {
(path, MacroKind::Derive, false, &[][..], false)
}
InvocationKind::DeriveContainer { ref derives, .. } => {
// Block expansion of the container until we resolve all derives in it.
// This is required for two reasons:
Expand Down Expand Up @@ -299,8 +304,17 @@ impl<'a> ResolverExpand for Resolver<'a> {

// Derives are not included when `invocations` are collected, so we have to add them here.
let parent_scope = &ParentScope { derives, ..parent_scope };
let require_inert = !invoc.fragment_kind.supports_macro_expansion();
let node_id = self.lint_node_id(eager_expansion_root);
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, node_id, force)?;
let (ext, res) = self.smart_resolve_macro_path(
path,
kind,
require_inert,
inner_attr,
parent_scope,
node_id,
force,
)?;

let span = invoc.span();
invoc_id.set_expn_data(ext.expn_data(
Expand All @@ -318,29 +332,6 @@ impl<'a> ResolverExpand for Resolver<'a> {
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
}

match invoc.fragment_kind {
AstFragmentKind::Arms
| AstFragmentKind::Fields
| AstFragmentKind::FieldPats
| AstFragmentKind::GenericParams
| AstFragmentKind::Params
| AstFragmentKind::StructFields
| AstFragmentKind::Variants => {
if let Res::Def(..) = res {
self.session.span_err(
span,
&format!(
"expected an inert attribute, found {} {}",
res.article(),
res.descr()
),
);
return Ok(InvocationRes::Single(self.dummy_ext(kind)));
}
}
_ => {}
}

Ok(InvocationRes::Single(ext))
}

Expand Down Expand Up @@ -403,18 +394,21 @@ impl<'a> ResolverExpand for Resolver<'a> {

impl<'a> Resolver<'a> {
/// Resolve macro path with error reporting and recovery.
/// Uses dummy syntax extensions for unresolved macros or macros with unexpected resolutions
/// for better error recovery.
fn smart_resolve_macro_path(
&mut self,
path: &ast::Path,
kind: MacroKind,
require_inert: bool,
inner_attr: bool,
parent_scope: &ParentScope<'a>,
node_id: NodeId,
force: bool,
) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force)
{
Ok((Some(ext), res)) => (ext, res),
// Use dummy syntax extensions for unresolved macros for better recovery.
Ok((None, res)) => (self.dummy_ext(kind), res),
Err(Determinacy::Determined) => (self.dummy_ext(kind), Res::Err),
Err(Determinacy::Undetermined) => return Err(Indeterminate),
Expand Down Expand Up @@ -451,19 +445,43 @@ impl<'a> Resolver<'a> {

self.check_stability_and_deprecation(&ext, path, node_id);

Ok(if ext.macro_kind() != kind {
let expected = kind.descr_expected();
let unexpected_res = if ext.macro_kind() != kind {
Some((kind.article(), kind.descr_expected()))
} else if require_inert && matches!(res, Res::Def(..)) {
Some(("a", "non-macro attribute"))
} else {
None
};
if let Some((article, expected)) = unexpected_res {
let path_str = pprust::path_to_string(path);
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
self.session
.struct_span_err(path.span, &msg)
.span_label(path.span, format!("not {} {}", kind.article(), expected))
.span_label(path.span, format!("not {} {}", article, expected))
.emit();
// Use dummy syntax extensions for unexpected macro kinds for better recovery.
(self.dummy_ext(kind), Res::Err)
} else {
(ext, res)
})
return Ok((self.dummy_ext(kind), Res::Err));
}

// We are trying to avoid reporting this error if other related errors were reported.
if inner_attr
&& !self.session.features_untracked().custom_inner_attributes
&& path != &sym::test
&& res != Res::Err
{
feature_err(
&self.session.parse_sess,
sym::custom_inner_attributes,
path.span,
match res {
Res::Def(..) => "inner macro attributes are unstable",
Res::NonMacroAttr(..) => "custom inner attributes are unstable",
_ => unreachable!(),
},
)
.emit();
}

Ok((ext, res))
}

pub fn resolve_macro_path(
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/attrs-resolution-errors.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
enum FooEnum {
#[test]
//~^ ERROR expected an inert attribute, found an attribute macro
//~^ ERROR expected non-macro attribute, found attribute macro
Bar(i32),
}

struct FooStruct {
#[test]
//~^ ERROR expected an inert attribute, found an attribute macro
//~^ ERROR expected non-macro attribute, found attribute macro
bar: i32,
}

Expand All @@ -21,20 +21,20 @@ fn main() {
match foo_struct {
FooStruct {
#[test] bar
//~^ ERROR expected an inert attribute, found an attribute macro
//~^ ERROR expected non-macro attribute, found attribute macro
} => {}
}

match 1 {
0 => {}
#[test]
//~^ ERROR expected an inert attribute, found an attribute macro
//~^ ERROR expected non-macro attribute, found attribute macro
_ => {}
}

let _another_foo_strunct = FooStruct {
#[test]
//~^ ERROR expected an inert attribute, found an attribute macro
//~^ ERROR expected non-macro attribute, found attribute macro
bar: 1,
};
}
30 changes: 15 additions & 15 deletions src/test/ui/attrs-resolution-errors.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
error: expected an inert attribute, found an attribute macro
--> $DIR/attrs-resolution-errors.rs:2:5
error: expected non-macro attribute, found attribute macro `test`
--> $DIR/attrs-resolution-errors.rs:2:7
|
LL | #[test]
| ^^^^^^^
| ^^^^ not a non-macro attribute

error: expected an inert attribute, found an attribute macro
--> $DIR/attrs-resolution-errors.rs:8:5
error: expected non-macro attribute, found attribute macro `test`
--> $DIR/attrs-resolution-errors.rs:8:7
|
LL | #[test]
| ^^^^^^^
| ^^^^ not a non-macro attribute

error: expected an inert attribute, found an attribute macro
--> $DIR/attrs-resolution-errors.rs:23:13
error: expected non-macro attribute, found attribute macro `test`
--> $DIR/attrs-resolution-errors.rs:23:15
|
LL | #[test] bar
| ^^^^^^^
| ^^^^ not a non-macro attribute

error: expected an inert attribute, found an attribute macro
--> $DIR/attrs-resolution-errors.rs:30:9
error: expected non-macro attribute, found attribute macro `test`
--> $DIR/attrs-resolution-errors.rs:30:11
|
LL | #[test]
| ^^^^^^^
| ^^^^ not a non-macro attribute

error: expected an inert attribute, found an attribute macro
--> $DIR/attrs-resolution-errors.rs:36:9
error: expected non-macro attribute, found attribute macro `test`
--> $DIR/attrs-resolution-errors.rs:36:11
|
LL | #[test]
| ^^^^^^^
| ^^^^ not a non-macro attribute

error: aborting due to 5 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/proc-macro-gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
extern crate test_macros;

fn _test_inner() {
#![empty_attr] //~ ERROR: non-builtin inner attributes are unstable
#![empty_attr] //~ ERROR: inner macro attributes are unstable
}

mod _test2_inner {
#![empty_attr] //~ ERROR: non-builtin inner attributes are unstable
#![empty_attr] //~ ERROR: inner macro attributes are unstable
}

#[empty_attr = "y"] //~ ERROR: key-value macro attributes are not supported
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/proc-macro/proc-macro-gates.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error[E0658]: non-builtin inner attributes are unstable
--> $DIR/proc-macro-gates.rs:10:5
error[E0658]: inner macro attributes are unstable
--> $DIR/proc-macro-gates.rs:10:8
|
LL | #![empty_attr]
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^
|
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable

error[E0658]: non-builtin inner attributes are unstable
--> $DIR/proc-macro-gates.rs:14:5
error[E0658]: inner macro attributes are unstable
--> $DIR/proc-macro-gates.rs:14:8
|
LL | #![empty_attr]
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^
|
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/proc-macro-gates2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ extern crate test_macros;
// should either require a feature gate or not be allowed on stable.

fn _test6<#[empty_attr] T>() {}
//~^ ERROR: expected an inert attribute, found an attribute macro
//~^ ERROR: expected non-macro attribute, found attribute macro

fn _test7() {
match 1 {
#[empty_attr] //~ ERROR: expected an inert attribute, found an attribute macro
#[empty_attr] //~ ERROR: expected non-macro attribute, found attribute macro
0 => {}
_ => {}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/proc-macro/proc-macro-gates2.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error: expected an inert attribute, found an attribute macro
--> $DIR/proc-macro-gates2.rs:12:11
error: expected non-macro attribute, found attribute macro `empty_attr`
--> $DIR/proc-macro-gates2.rs:12:13
|
LL | fn _test6<#[empty_attr] T>() {}
| ^^^^^^^^^^^^^
| ^^^^^^^^^^ not a non-macro attribute

error: expected an inert attribute, found an attribute macro
--> $DIR/proc-macro-gates2.rs:17:9
error: expected non-macro attribute, found attribute macro `empty_attr`
--> $DIR/proc-macro-gates2.rs:17:11
|
LL | #[empty_attr]
| ^^^^^^^^^^^^^
| ^^^^^^^^^^ not a non-macro attribute

error: aborting due to 2 previous errors

Loading