Skip to content
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

Rollup of 9 pull requests #88584

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
af1b65c
Path remapping: Make behavior of diagnostics output dependent on pres…
michaelwoerister Aug 26, 2021
8436fe1
Add test for showing remapped path in diagnostics
cbeuw Aug 20, 2021
c296c89
Fix remap-path-prefix UI test case.
michaelwoerister Aug 27, 2021
c6d800d
Point at unclosed delimiters as part of the primary MultiSpan
estebank Aug 27, 2021
d7da82a
expand: Treat more macro calls as statement macro calls
petrochenkov Aug 26, 2021
b99038f
use `unwrap_unchecked` where possible
ibraheemdev Aug 30, 2021
99a3d64
Remove single use variables
ptrojahn Aug 31, 2021
ffc43b8
add safety annotation to `LinkedList::detach_all_nodes`
ibraheemdev Aug 31, 2021
7189c85
Improve closure dummy capture suggestion in macros.
m-ou-se Aug 31, 2021
7d18052
Add test for closure migration where body is a block fragment.
m-ou-se Aug 31, 2021
59b245e
fix(rustc_lint): better detect when parens are necessary
notriddle Aug 31, 2021
6c9e708
`fmt::Formatter::pad`: don't call chars().count() more than one time
klensy Sep 1, 2021
f5f489b
fix clippy lints
klensy Sep 1, 2021
a5fd955
add regression test for issue 83190
lqd Sep 1, 2021
d9047db
Rollup merge of #88363 - michaelwoerister:remapped-diagnostics, r=est…
GuillaumeGomez Sep 2, 2021
2693e12
Rollup merge of #88386 - estebank:unmatched-delims, r=jackh726
GuillaumeGomez Sep 2, 2021
2f22cad
Rollup merge of #88428 - petrochenkov:stmtid, r=Aaron1011
GuillaumeGomez Sep 2, 2021
c892775
Rollup merge of #88505 - ibraheemdev:use-unwrap-unchecked, r=kennytm
GuillaumeGomez Sep 2, 2021
1d1e931
Rollup merge of #88532 - ptrojahn:single_use, r=davidtwco
GuillaumeGomez Sep 2, 2021
b341e56
Rollup merge of #88543 - m-ou-se:closure-migration-macro-block-fragme…
GuillaumeGomez Sep 2, 2021
60eb925
Rollup merge of #88547 - notriddle:notriddle/is-expr-delims-necessary…
GuillaumeGomez Sep 2, 2021
a90fff6
Rollup merge of #88560 - klensy:formatter-pad-shrink, r=m-ou-se
GuillaumeGomez Sep 2, 2021
7e3855b
Rollup merge of #88565 - lqd:issue-83190, r=spastorino
GuillaumeGomez Sep 2, 2021
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
12 changes: 11 additions & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,17 @@ pub fn noop_flat_map_stmt<T: MutVisitor>(
) -> SmallVec<[Stmt; 1]> {
vis.visit_id(&mut id);
vis.visit_span(&mut span);
noop_flat_map_stmt_kind(kind, vis).into_iter().map(|kind| Stmt { id, kind, span }).collect()
let stmts: SmallVec<_> = noop_flat_map_stmt_kind(kind, vis)
.into_iter()
.map(|kind| Stmt { id, kind, span })
.collect();
if stmts.len() > 1 {
panic!(
"cloning statement `NodeId`s is prohibited by default, \
the visitor should implement custom statement visiting"
);
}
stmts
}

pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'tcx> DebugContext<'tcx> {
rustc_interface::util::version_str().unwrap_or("unknown version"),
cranelift_codegen::VERSION,
);
let comp_dir = tcx.sess.opts.working_dir.to_string_lossy(false).into_owned();
let comp_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped).into_owned();
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
Some(path) => {
let name = path.to_string_lossy().into_owned();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ mod vtable;
mod prelude {
pub(crate) use std::convert::{TryFrom, TryInto};

pub(crate) use rustc_span::Span;
pub(crate) use rustc_span::{Span, FileNameDisplayPreference};

pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
pub(crate) use rustc_middle::bug;
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use rustc_middle::ty::{self, AdtKind, GeneratorSubsts, ParamEnv, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::config::{self, DebugInfo};
use rustc_span::symbol::{Interner, Symbol};
use rustc_span::FileNameDisplayPreference;
use rustc_span::{self, SourceFile, SourceFileHash, Span};
use rustc_target::abi::{Abi, Align, HasDataLayout, Integer, LayoutOf, TagEncoding};
use rustc_target::abi::{Int, Pointer, F32, F64};
Expand Down Expand Up @@ -771,7 +772,13 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll
let hash = Some(&source_file.src_hash);
let file_name = Some(source_file.name.prefer_remapped().to_string());
let directory = if source_file.is_real_file() && !source_file.is_imported() {
Some(cx.sess().opts.working_dir.to_string_lossy(false).to_string())
Some(
cx.sess()
.opts
.working_dir
.to_string_lossy(FileNameDisplayPreference::Remapped)
.to_string(),
)
} else {
// If the path comes from an upstream crate we assume it has been made
// independent of the compiler's working directory one way or another.
Expand Down Expand Up @@ -999,7 +1006,7 @@ pub fn compile_unit_metadata(
let producer = format!("clang LLVM ({})", rustc_producer);

let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(false);
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
let flags = "\0";
let output_filenames = tcx.output_filenames(());
let out_dir = &output_filenames.out_directory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl AnnotateSnippetEmitterWriter {
}
// owned: line source, line index, annotations
type Owned = (String, usize, Vec<crate::snippet::Annotation>);
let filename = primary_lo.file.name.prefer_local();
let filename = source_map.filename_for_diagnostics(&primary_lo.file.name);
let origin = filename.to_string_lossy();
let annotated_files: Vec<Owned> = annotated_files
.into_iter()
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ impl EmitterWriter {
buffer_msg_line_offset,
&format!(
"{}:{}:{}",
loc.file.name.prefer_local(),
sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1,
),
Expand All @@ -1334,7 +1334,7 @@ impl EmitterWriter {
0,
&format!(
"{}:{}:{}: ",
loc.file.name.prefer_local(),
sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1,
),
Expand Down Expand Up @@ -1362,12 +1362,12 @@ impl EmitterWriter {
};
format!(
"{}:{}{}",
annotated_file.file.name.prefer_local(),
sm.filename_for_diagnostics(&annotated_file.file.name),
sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index),
col
)
} else {
format!("{}", annotated_file.file.name.prefer_local())
format!("{}", sm.filename_for_diagnostics(&annotated_file.file.name))
};
buffer.append(buffer_msg_line_offset + 1, &loc, Style::LineAndColumn);
for _ in 0..max_line_num_len {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl DiagnosticSpan {
});

DiagnosticSpan {
file_name: start.file.name.prefer_local().to_string(),
file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
line_start: start.line,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ impl<'a> ExtCtxt<'a> {
span,
&format!(
"cannot resolve relative path in non-file source `{}`",
other.prefer_local()
self.source_map().filename_for_diagnostics(&other)
),
));
}
Expand Down
100 changes: 67 additions & 33 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::tokenstream::TokenStream;
use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::{AstLike, Block, Inline, ItemKind, Local, MacArgs, MacCall};
use rustc_ast::{AstLike, Block, Inline, ItemKind, MacArgs, MacCall};
use rustc_ast::{MacCallStmt, MacStmtStyle, MetaItemKind, ModKind, NestedMetaItem};
use rustc_ast::{NodeId, PatKind, Path, StmtKind, Unsafe};
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -559,7 +559,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.cx.force_mode = orig_force_mode;

// Finally incorporate all the expanded macros into the input AST fragment.
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
let mut placeholder_expander = PlaceholderExpander::default();
while let Some(expanded_fragments) = expanded_fragments.pop() {
for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
placeholder_expander
Expand Down Expand Up @@ -1061,13 +1061,51 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
attr
}

fn take_stmt_bang(
&mut self,
stmt: ast::Stmt,
) -> Result<(bool, MacCall, Vec<ast::Attribute>), ast::Stmt> {
match stmt.kind {
StmtKind::MacCall(mac) => {
let MacCallStmt { mac, style, attrs, .. } = mac.into_inner();
Ok((style == MacStmtStyle::Semicolon, mac, attrs.into()))
}
StmtKind::Item(ref item) if matches!(item.kind, ItemKind::MacCall(..)) => {
match stmt.kind {
StmtKind::Item(item) => match item.into_inner() {
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
Ok((mac.args.need_semicolon(), mac, attrs))
}
_ => unreachable!(),
},
_ => unreachable!(),
}
}
StmtKind::Semi(ref expr) if matches!(expr.kind, ast::ExprKind::MacCall(..)) => {
match stmt.kind {
StmtKind::Semi(expr) => match expr.into_inner() {
ast::Expr { kind: ast::ExprKind::MacCall(mac), attrs, .. } => {
Ok((mac.args.need_semicolon(), mac, attrs.into()))
}
_ => unreachable!(),
},
_ => unreachable!(),
}
}
StmtKind::Local(..) | StmtKind::Empty | StmtKind::Item(..) | StmtKind::Semi(..) => {
Err(stmt)
}
StmtKind::Expr(..) => unreachable!(),
}
}

fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
self.cfg.configure(node)
}

// Detect use of feature-gated or invalid attributes on macro invocations
// since they will not be detected after macro expansion.
fn check_attributes(&mut self, attrs: &[ast::Attribute], call: &MacCall) {
fn check_attributes(&self, attrs: &[ast::Attribute], call: &MacCall) {
let features = self.cx.ecfg.features.unwrap();
let mut attrs = attrs.iter().peekable();
let mut span: Option<Span> = None;
Expand Down Expand Up @@ -1177,11 +1215,6 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
});
}

// This is needed in order to set `lint_node_id` for `let` statements
fn visit_local(&mut self, local: &mut P<Local>) {
assign_id!(self, &mut local.id, || noop_visit_local(local, self));
}

fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
let mut arm = configure!(self, arm);

Expand Down Expand Up @@ -1299,31 +1332,39 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
let mut stmt = configure!(self, stmt);

// we'll expand attributes on expressions separately
if !stmt.is_expr() {
// We pull macro invocations (both attributes and fn-like macro calls) out of their
// `StmtKind`s and treat them as statement macro invocations, not as items or expressions.
// FIXME: invocations in semicolon-less expressions positions are expanded as expressions,
// changing that requires some compatibility measures.
let mut stmt = if !stmt.is_expr() {
if let Some(attr) = self.take_first_attr(&mut stmt) {
return self
.collect_attr(attr, Annotatable::Stmt(P(stmt)), AstFragmentKind::Stmts)
.make_stmts();
}
}

if let StmtKind::MacCall(mac) = stmt.kind {
let MacCallStmt { mac, style, attrs, tokens: _ } = mac.into_inner();
self.check_attributes(&attrs, &mac);
let mut placeholder =
self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts).make_stmts();

// If this is a macro invocation with a semicolon, then apply that
// semicolon to the final statement produced by expansion.
if style == MacStmtStyle::Semicolon {
if let Some(stmt) = placeholder.pop() {
placeholder.push(stmt.add_trailing_semicolon());
let span = stmt.span;
match self.take_stmt_bang(stmt) {
Ok((add_semicolon, mac, attrs)) => {
self.check_attributes(&attrs, &mac);
let mut stmts =
self.collect_bang(mac, span, AstFragmentKind::Stmts).make_stmts();

// If this is a macro invocation with a semicolon, then apply that
// semicolon to the final statement produced by expansion.
if add_semicolon {
if let Some(stmt) = stmts.pop() {
stmts.push(stmt.add_trailing_semicolon());
}
}

return stmts;
}
Err(stmt) => stmt,
}

return placeholder;
}
} else {
stmt
};

// The only way that we can end up with a `MacCall` expression statement,
// (as opposed to a `StmtKind::MacCall`) is if we have a macro as the
Expand All @@ -1338,14 +1379,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}
}

// The placeholder expander gives ids to statements, so we avoid folding the id here.
// We don't use `assign_id!` - it will be called when we visit statement's contents
// (e.g. an expression, item, or local)
let ast::Stmt { id, kind, span } = stmt;
let res = noop_flat_map_stmt_kind(kind, self)
.into_iter()
.map(|kind| ast::Stmt { id, kind, span })
.collect();
let res = assign_id!(self, &mut stmt.id, || noop_flat_map_stmt(stmt, self));

self.cx.current_expansion.is_trailing_mac = false;
res
Expand Down
25 changes: 4 additions & 21 deletions compiler/rustc_expand/src/placeholders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::base::ExtCtxt;
use crate::expand::{AstFragment, AstFragmentKind};

use rustc_ast as ast;
Expand Down Expand Up @@ -175,17 +174,12 @@ pub fn placeholder(
}
}

pub struct PlaceholderExpander<'a, 'b> {
#[derive(Default)]
pub struct PlaceholderExpander {
expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
cx: &'a mut ExtCtxt<'b>,
monotonic: bool,
}

impl<'a, 'b> PlaceholderExpander<'a, 'b> {
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
PlaceholderExpander { cx, expanded_fragments: FxHashMap::default(), monotonic }
}

impl PlaceholderExpander {
pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
fragment.mut_visit_with(self);
self.expanded_fragments.insert(id, fragment);
Expand All @@ -196,7 +190,7 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> {
}
}

impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
impl MutVisitor for PlaceholderExpander {
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
if arm.is_placeholder {
self.remove(arm.id).make_arms()
Expand Down Expand Up @@ -360,15 +354,4 @@ impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
_ => noop_visit_ty(ty, self),
}
}

fn visit_block(&mut self, block: &mut P<ast::Block>) {
noop_visit_block(block, self);

for stmt in block.stmts.iter_mut() {
if self.monotonic {
assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
stmt.id = self.cx.resolver.next_node_id();
}
}
}
}
9 changes: 3 additions & 6 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,14 +1630,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
(TypeError::Sorts(values), extra) => {
let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
(true, ty::Opaque(def_id, _)) => {
let pos = self
.tcx
.sess
.source_map()
.lookup_char_pos(self.tcx.def_span(*def_id).lo());
let sm = self.tcx.sess.source_map();
let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
format!(
" (opaque type at <{}:{}:{}>)",
pos.file.name.prefer_local(),
sm.filename_for_diagnostics(&pos.file.name),
pos.line,
pos.col.to_usize() + 1,
)
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,16 @@ trait UnusedDelimLint {
let lhs_needs_parens = {
let mut innermost = inner;
loop {
if let ExprKind::Binary(_, lhs, _rhs) = &innermost.kind {
innermost = lhs;
if !classify::expr_requires_semi_to_be_stmt(innermost) {
break true;
}
} else {
break false;
innermost = match &innermost.kind {
ExprKind::Binary(_, lhs, _rhs) => lhs,
ExprKind::Call(fn_, _params) => fn_,
ExprKind::Cast(expr, _ty) => expr,
ExprKind::Type(expr, _ty) => expr,
ExprKind::Index(base, _subscript) => base,
_ => break false,
};
if !classify::expr_requires_semi_to_be_stmt(innermost) {
break true;
}
}
};
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ impl<'tcx> Body<'tcx> {
/// Returns an iterator over all function arguments.
#[inline]
pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
let arg_count = self.arg_count;
(1..arg_count + 1).map(Local::new)
(1..self.arg_count + 1).map(Local::new)
}

/// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
Expand All @@ -421,9 +420,7 @@ impl<'tcx> Body<'tcx> {
pub fn vars_and_temps_iter(
&self,
) -> impl DoubleEndedIterator<Item = Local> + ExactSizeIterator {
let arg_count = self.arg_count;
let local_count = self.local_decls.len();
(arg_count + 1..local_count).map(Local::new)
(self.arg_count + 1..self.local_decls.len()).map(Local::new)
}

#[inline]
Expand Down
Loading