8000 Remove Spans from HIR -- 2/N -- Small HIR nodes by cjgillot · Pull Request #73094 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Remove Spans from HIR -- 2/N -- Small HIR nodes #73094

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 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
625594f
Remove useless Clone bound in IndexVec.
cjgillot May 19, 2020
6758a85
Introduce HirIdVec.
cjgillot May 19, 2020
0b793a3
Collect spans during lowering.
cjgillot May 1, 2020
e69a8df
Introduce a query for HIR spans.
cjgillot May 1, 2020
0d98c91
Don't pass spans in hir::map::blocks.
cjgillot May 2, 2020
3947c9e
Stop passing the Span in HIR visiting.
cjgillot May 2, 2020
973d19b
Pass HirId in save_analysis.
cjgillot Jun 7, 2020
700d697
Remove Span parameter from HIR collector.
cjgillot Jun 3, 2020
05dc82a
Fix fulldeps tests.
cjgillot Dec 13, 2020
7b9999c
Fix clippy.
cjgillot Dec 13, 2020
3dd2859
Remove span from hir::Param.
cjgillot May 1, 2020
5dada24
Remove span from hir::Variant.
cjgillot May 1, 2020
e92e9f7
Remove span from hir::StructField.
cjgillot May 1, 2020
f94bbef
Remove span from hir::Stmt.
cjgillot May 1, 2020
2bc251c
Remove span from hir::Block.
cjgillot May 1, 2020
fcae632
Remove span from hir::MacroDef.
cjgillot May 2, 2020
1641026
Remove span from hir::GenericParam.
cjgillot May 2, 2020
521bc45
Remove span from hir::Arm.
cjgillot May 2, 2020
0373228
Remove span from hir::FieldPat.
cjgillot May 2, 2020
afc6e69
Remove span from hir::Local.
cjgillot May 2, 2020
732ae69
Remove span from hir::Pat.
cjgillot May 2, 2020
96b8564
Remove span from hir::Field.
cjgillot May 2, 2020
e6c0698
Remove GenericArg::span.
cjgillot May 3, 2020
4491f82
Remove Span from hir::TypeBinding.
cjgillot May 8, 2020
fdd2c7d
Remove Span from hir::ConstArg.
cjgillot Jun 1, 2020
ba8fc92
Remove Span from hir::TraitItemRef.
cjgillot Jun 1, 2020
272df28
Remove Span from hir::ImplItemRef.
cjgillot Jun 1, 2020
a0b4ad1
Remove span from hir::ForeignItemRef.
cjgillot Dec 11, 2020
b604b5e
Fix fulldeps tests.
cjgillot May 9, 2020
7ec01a6
Fortify find_entry.
cjgillot Dec 13, 2020
304bdec
Remove Span from hir::CrateItem.
cjgillot Dec 10, 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
Remove span from hir::Pat.
  • Loading branch information
cjgillot committed Jan 6, 2021
commit 732ae6959bf47eb208b8c9462e1378d45958d740
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// parameters (c.f. rust-lang/rust#64512).
for (index, parameter) in decl.inputs.iter().enumerate() {
let parameter = this.lower_param(parameter);
let span = parameter.pat.span;
let span = this.spans[parameter.pat.hir_id];

// Check if this is a binding pattern, if so, we can optimize and avoid adding a
// `let <pat> = __argN;` statement. In this case, we do not rename the parameter.
Expand Down
9 changes: 1 addition & 8 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.arena.alloc(hir::Pat {
hir_id,
kind: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None),
span,
default_binding_modes: true,
}),
hir_id,
Expand All @@ -2595,19 +2594,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
self.arena.alloc(hir::Pat {
hir_id: self.next_id(span),
kind,
span,
default_binding_modes: true,
})
self.arena.alloc(hir::Pat { hir_id: self.next_id(span), kind, default_binding_modes: true })
}

fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
self.arena.alloc(hir::Pat {
hir_id: self.next_id(span),
kind,
span,
default_binding_modes: false,
})
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.arena.alloc(hir::Pat {
hir_id: self.lower_node_id(p.id, p.span),
kind,
span: p.span,
default_binding_modes: true,
})
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,6 @@ pub struct Pat<'hir> {
#[stable_hasher(ignore)]
pub hir_id: HirId,
pub kind: PatKind<'hir>,
pub span: Span,
// Whether to use default binding modes.
// At present, this is false only for destructuring assignment.
pub default_binding_modes: bool,
Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_hir/src/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::def::{CtorOf, DefKind, Res};
use crate::def_id::DefId;
use crate::hir::{self, HirId, PatKind};
use rustc_span::symbol::Ident;
use rustc_span::Span;

use std::iter::{Enumerate, ExactSizeIterator};

Expand Down Expand Up @@ -60,10 +59,10 @@ impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
impl hir::Pat<'_> {
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
/// `match foo() { Some(a) => (), None => () }`
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Span, Ident)) {
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Ident)) {
self.walk_always(|p| {
if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
f(binding_mode, p.hir_id, p.span, ident);
f(binding_mode, p.hir_id, ident);
}
});
}
Expand All @@ -72,17 +71,14 @@ impl hir::Pat<'_> {
/// `match foo() { Some(a) => (), None => () }`.
///
/// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
pub fn each_binding_or_first(
&self,
f: &mut impl FnMut(hir::BindingAnnotation, HirId, Span, Ident),
) {
pub fn each_binding_or_first(&self, f: &mut impl FnMut(hir::BindingAnnotation, HirId, Ident)) {
self.walk(|p| match &p.kind {
PatKind::Or(ps) => {
ps[0].each_binding_or_first(f);
false
}
PatKind::Binding(bm, _, ident, _) => {
f(*bm, p.hir_id, p.span, *ident);
f(*bm, p.hir_id, *ident);
true
}
_ => true,
Expand Down Expand Up @@ -150,7 +146,7 @@ impl hir::Pat<'_> {
// ref bindings are be implicit after #42640 (default match binding modes). See issue #44848.
pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
let mut result = None;
self.each_binding(|annotation, _, _, _| match annotation {
self.each_binding(|annotation, _, _| match annotation {
hir::BindingAnnotation::Ref => match result {
None | Some(hir::Mutability::Not) => result = Some(hir::Mutability::Not),
_ => {}
Expand Down
18 changes: 11 additions & 7 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,29 @@ impl<'a> State<'a> {
pub fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
where
F: FnMut(&mut State<'_>, &T),
G: FnMut(&T) -> rustc_span::Span,
G: FnMut(&State<'_>, &T) -> rustc_span::Span,
{
self.rbox(0, b);
let len = elts.len();
let mut i = 0;
for elt in elts {
self.maybe_print_comment(get_span(elt).hi());
self.maybe_print_comment(get_span(self, elt).hi());
op(self, elt);
i += 1;
if i < len {
self.s.word(",");
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
self.maybe_print_trailing_comment(
get_span(self, elt),
Some(get_span(self, &elts[i]).hi()),
);
self.space_if_not_bol();
}
}
self.end();
}

pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr<'_>]) {
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span)
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |_, e| e.span)
}

pub fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[ast::Attribute]) {
Expand Down Expand Up @@ -1186,7 +1189,7 @@ impl<'a> State<'a> {
s.print_expr(&field.expr);
s.end()
},
|f| f.span,
|_, f| f.span,
);
match *wth {
Some(ref expr) => {
Expand Down Expand Up @@ -1836,7 +1839,8 @@ impl<'a> State<'a> {
}

pub fn print_pat(&mut self, pat: &hir::Pat<'_>) {
self.maybe_print_comment(pat.span.lo());
let span = self.span(pat.hir_id);
self.maybe_print_comment(span.lo());
self.ann.pre(self, AnnNode::Pat(pat));
// Pat isn't normalized, but the beauty of it
// is that it doesn't matter
Expand Down Expand Up @@ -1900,7 +1904,7 @@ impl<'a> State<'a> {
s.print_pat(&f.pat);
s.end()
},
|f| f.pat.span,
|s, f| s.span(f.pat.hir_id),
);
if etc {
if !fields.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
local_visitor.visit_expr(expr);
}
let err_span = if let Some(pattern) = local_visitor.found_arg_pattern {
pattern.span
self.tcx.hir().span(pattern.hir_id)
} else if let Some(span) = arg_data.span {
// `span` here lets us point at `sum` instead of the entire right hand side expr:
// error[E0282]: type annotations needed
Expand Down Expand Up @@ -537,12 +537,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// with the type parameter `_` specified
// ```
err.span_label(
pattern.span,
self.tcx.hir().span(pattern.hir_id),
format!("consider giving this closure parameter {}", suffix),
);
} else if let Some(pattern) = local_visitor.found_local_pattern {
let pattern_span = self.tcx.hir().span(pattern.hir_id);
let msg 10000 = if let Some(simple_ident) = pattern.simple_ident() {
match pattern.span.desugaring_kind() {
match pattern_span.desugaring_kind() {
None => format!("consider giving `{}` {}", simple_ident, suffix),
Some(DesugaringKind::ForLoop(_)) => {
"the element type for this iterator is not specified".to_string()
Expand All @@ -552,7 +553,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
} else {
format!("consider giving this pattern {}", suffix)
};
err.span_label(pattern.span, msg);
err.span_label(pattern_span, msg);
} else if let Some(e) = local_visitor.found_method_call {
if let ExprKind::MethodCall(segment, ..) = &e.kind {
// Suggest specifying type params or point out the return type of the call:
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
argument_scope: region::Scope,
ast_body: &'tcx hir::Expr<'tcx>,
) -> BlockAnd<()> {
let tcx = self.hir.tcx();

// Allocate locals for the function arguments
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
let source_info =
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| tcx.hir().span(arg.pat.hir_id)));
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));

// If this is a simple binding pattern, give debuginfo a nice name.
Expand Down Expand Up @@ -859,8 +861,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
name = ident.name;
let pat_span = tcx_hir.span(pat.hir_id);
match hir_typeck_results
.extract_binding_mode(tcx.sess, pat.hir_id, pat.span)
.extract_binding_mode(tcx.sess, pat.hir_id, pat_span)
{
Some(ty::BindByValue(hir::Mutability::Mut)) => {
mutability = Mutability::Mut;
Expand Down Expand Up @@ -904,7 +907,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// Make sure we drop (parts of) the argument even when not matched on.
self.schedule_drop(
arg_opt.as_ref().map_or(ast_body.span, |arg| arg.pat.span),
arg_opt.as_ref().map_or(ast_body.span, |arg| tcx_hir.span(arg.pat.hir_id)),
argument_scope,
local,
DropKind::Value,
Expand Down
44 changes: 26 additions & 18 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
let pattern: &_ = cx.pattern_arena.alloc(expand_pattern(pattern));
if !patcx.errors.is_empty() {
*have_errors = true;
patcx.report_inlining_errors(pat.span);
let pat_span = self.tcx.hir().span(pat.hir_id);
patcx.report_inlining_errors(pat_span);
}
(pattern, pattern_ty)
}
Expand Down Expand Up @@ -226,9 +227,10 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
}

let joined_patterns = joined_uncovered_patterns(&witnesses);
let pat_span = self.tcx.hir().span(pat.hir_id);
let mut err = struct_span_err!(
self.tcx.sess,
pat.span,
pat_span,
E0005,
"refutable pattern in {}: {} not covered",
origin,
Expand All @@ -242,7 +244,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
false
}
_ => {
err.span_label(pat.span, pattern_not_covered_label(&witnesses, &joined_patterns));
err.span_label(pat_span, pattern_not_covered_label(&witnesses, &joined_patterns));
true
}
};
Expand Down Expand Up @@ -281,13 +283,14 @@ fn const_not_var(
path: &hir::Path<'_>,
) {
let descr = path.res.descr();
let pat_span = tcx.hir().span(pat.hir_id);
err.span_label(
pat.span,
pat_span,
format!("interpreted as {} {} pattern, not a new variable", path.res.article(), descr,),
);

err.span_suggestion(
pat.span,
pat_span,
"introduce a variable instead",
format!("{}_var", path.segments[0].ident).to_lowercase(),
// Cannot use `MachineApplicable` as it's not really *always* correct
Expand All @@ -304,8 +307,9 @@ fn const_not_var(
fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_>) {
pat.walk_always(|p| {
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
let span = cx.tcx.hir().span(p.hir_id);
if let Some(ty::BindByValue(hir::Mutability::Not)) =
cx.typeck_results.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
cx.typeck_results.extract_binding_mode(cx.tcx.sess, p.hir_id, span)
{
let pat_ty = cx.typeck_results.pat_ty(p).peel_refs();
if let ty::Adt(edef, _) = pat_ty.kind() {
Expand All @@ -317,7 +321,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
cx.tcx.struct_span_lint_hir(
BINDINGS_WITH_VARIANT_NAME,
p.hir_id,
p.span,
span,
|lint| {
let ty_path = cx.tcx.def_path_str(edef.did);
lint.build(&format!(
Expand All @@ -327,7 +331,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
))
.code(error_code!(E0170))
.span_suggestion(
p.span,
span,
"to match on the variant, qualify the path",
format!("{}::{}", ty_path, ident),
Applicability::MachineApplicable,
Expand Down Expand Up @@ -616,17 +620,19 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
hir::PatKind::Binding(.., name, Some(sub)) => (*name, sub),
_ => return,
};
let binding_span = pat.span.with_hi(name.span.hi());
let pat_span = cx.tcx.hir().span(pat.hir_id);
let binding_span = pat_span.with_hi(name.span.hi());

let typeck_results = cx.typeck_results;
let sess = cx.tcx.sess;

// Get the binding move, extract the mutability if by-ref.
let mut_outer = match typeck_results.extract_binding_mode(sess, pat.hir_id, pat.span) {
Some(ty::BindByValue(_)) if is_binding_by_move(cx, pat.hir_id, pat.span) => {
let mut_outer = match typeck_results.extract_binding_mode(sess, pat.hir_id, pat_span) {
Some(ty::BindByValue(_)) if is_binding_by_move(cx, pat.hir_id, pat_span) => {
// We have `x @ pat` where `x` is by-move. Reject all borrows in `pat`.
let mut conflicts_ref = Vec::new();
sub.each_binding(|_, hir_id, span, _| {
sub.each_binding(|_, hir_id, _| {
let span = cx.tcx.hir().span(hir_id);
match typeck_results.extract_binding_mode(sess, hir_id, span) {
Some(ty::BindByValue(_)) | None => {}
Some(ty::BindByReference(_)) => conflicts_ref.push(span),
Expand All @@ -638,7 +644,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
name,
typeck_results.node_type(pat.hir_id),
);
sess.struct_span_err(pat.span, "borrow of moved value")
sess.struct_span_err(pat_span, "borrow of moved value")
.span_label(binding_span, format!("value moved into `{}` here", name))
.span_label(binding_span, occurs_because)
.span_labels(conflicts_ref, "value borrowed here after move")
Expand All @@ -655,7 +661,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
let mut conflicts_move = Vec::new();
let mut conflicts_mut_mut = Vec::new();
let mut conflicts_mut_ref = Vec::new();
sub.each_binding(|_, hir_id, span, name| {
sub.each_binding(|_, hir_id, name| {
let span = cx.tcx.hir().span(hir_id);
match typeck_results.extract_binding_mode(sess, hir_id, span) {
Some(ty::BindByReference(mut_inner)) => match (mut_outer, mut_inner) {
(Mutability::Not, Mutability::Not) => {} // Both sides are `ref`.
Expand All @@ -673,7 +680,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
if !conflicts_mut_mut.is_empty() {
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
let mut err = sess
.struct_span_err(pat.span, "cannot borrow value as mutable more than once at a time");
.struct_span_err(pat_span, "cannot borrow value as mutable more than once at a time");
err.span_label(binding_span, format!("first mutable borrow, by `{}`, occurs here", name));
for (span, name) in conflicts_mut_mut {
err.span_label(span, format!("another mutable borrow, by `{}`, occurs here", name));
Expand All @@ -693,7 +700,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
};
let msg =
format!("cannot borrow value as {} because it is also borrowed as {}", also, primary);
let mut err = sess.struct_span_err(pat.span, &msg);
let mut err = sess.struct_span_err(pat_span, &msg);
err.span_label(binding_span, format!("{} borrow, by `{}`, occurs here", primary, name));
for (span, name) in conflicts_mut_ref {
err.span_label(span, format!("{} borrow, by `{}`, occurs here", also, name));
Expand All @@ -705,7 +712,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
} else if !conflicts_move.is_empty() {
// Report by-ref and by-move conflicts, e.g. `ref x @ y`.
let mut err =
sess.struct_span_err(pat.span, "cannot move out of value because it is borrowed");
sess.struct_span_err(pat_span, "cannot move out of value because it is borrowed");
err.span_label(binding_span, format!("value borrowed, by `{}`, here", name));
for (span, name) in conflicts_move {
err.span_label(span, format!("value moved into `{}` here", name));
Expand Down Expand Up @@ -735,10 +742,11 @@ fn check_legality_of_bindings_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pa
match pat.kind {
hir::PatKind::Binding(.., ref subpat) => {
if !self.bindings_allowed {
let pat_span = self.cx.tcx.hir().span(pat.hir_id);
feature_err(
&self.cx.tcx.sess.parse_sess,
sym::bindings_after_at,
pat.span,
pat_span,
"pattern bindings after an `@` are unstable",
)
.emit();
Expand Down
Loading
0