Skip to content

Commit dad5ec0

Browse files
committed
Remove span from hir::Variant.
1 parent a1b12d8 commit dad5ec0

File tree

15 files changed

+54
-45
lines changed

15 files changed

+54
-45
lines changed

src/librustc_ast_lowering/item.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
706706
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
707707
id: self.lower_node_id(v.id, v.span),
708708
ident: v.ident,
709-
span: v.span,
710709
}
711710
}
712711

src/librustc_hir/hir.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,8 +2291,6 @@ pub struct Variant<'hir> {
22912291
pub data: VariantData<'hir>,
22922292
/// Explicit discriminant (e.g., `Foo = 1`).
22932293
pub disr_expr: Option<AnonConst>,
2294-
/// Span
2295-
pub span: Span,
22962294
}
22972295

22982296
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]

src/librustc_hir_pretty/lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
77
use rustc_ast_pretty::pp::{self, Breaks};
88
use rustc_ast_pretty::pprust::{Comments, PrintState};
99
use rustc_hir as hir;
10-
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node};
10+
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdVec, Node};
1111
use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
1212
use rustc_span::source_map::{SourceMap, Spanned};
1313
use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol};
@@ -86,6 +86,7 @@ pub struct State<'a> {
8686
pub s: pp::Printer,
8787
comments: Option<Comments<'a>>,
8888
ann: &'a (dyn PpAnn + 'a),
89+
spans: &'a HirIdVec<rustc_span::Span>,
8990
}
9091

9192
impl<'a> State<'a> {
@@ -166,7 +167,7 @@ pub fn print_crate<'a>(
166167
input: String,
167168
ann: &'a dyn PpAnn,
168169
) -> String {
169-
let mut s = State::new_from_input(sm, filename, input, ann);
170+
let mut s = State::new_from_input(sm, filename, input, ann, &krate.spans);
170171

171172
// When printing the AST, we sometimes need to inject `#[no_std]` here.
172173
// Since you can't compile the HIR, it's not necessary.
@@ -182,16 +183,27 @@ impl<'a> State<'a> {
182183
filename: FileName,
183184
input: String,
184185
ann: &'a dyn PpAnn,
186+
spans: &'a HirIdVec<rustc_span::Span>,
185187
) -> State<'a> {
186-
State { s: pp::mk_printer(), comments: Some(Comments::new(sm, filename, input)), ann }
188+
State {
189+
s: pp::mk_printer(),
190+
comments: Some(Comments::new(sm, filename, input)),
191+
ann,
192+
spans,
193+
}
194+
}
195+
196+
fn span(&self, hir_id: hir::HirId) -> rustc_span::Span {
197+
self.spans.get(hir_id).copied().unwrap_or(rustc_span::DUMMY_SP)
187198
}
188199
}
189200

190201
pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
191202
where
192203
F: FnOnce(&mut State<'_>),
193204
{
194-
let mut printer = State { s: pp::mk_printer(), comments: None, ann };
205+
let spans = &HirIdVec::default();
206+
let mut printer = State { s: pp::mk_printer(), comments: None, ann, spans };
195207
f(&mut printer);
196208
printer.s.eof()
197209
}
@@ -829,14 +841,15 @@ impl<'a> State<'a> {
829841
pub fn print_variants(&mut self, variants: &[hir::Variant<'_>], span: rustc_span::Span) {
830842
self.bopen();
831843
for v in variants {
844+
let span = self.span(v.id);
832845
self.space_if_not_bol();
833-
self.maybe_print_comment(v.span.lo());
846+
self.maybe_print_comment(span.lo());
834847
self.print_outer_attributes(&v.attrs);
835848
self.ibox(INDENT_UNIT);
836849
self.print_variant(v);
837850
self.s.word(",");
838851
self.end();
839-
self.maybe_print_trailing_comment(v.span, None);
852+
self.maybe_print_trailing_comment(span, None);
840853
}
841854
self.bclose(span)
842855
}
@@ -923,7 +936,7 @@ impl<'a> State<'a> {
923936
pub fn print_variant(&mut self, v: &hir::Variant<'_>) {
924937
self.head("");
925938
let generics = hir::Generics::empty();
926-
self.print_struct(&v.data, &generics, v.ident.name, v.span, false);
939+
self.print_struct(&v.data, &generics, v.ident.name, self.span(v.id), false);
927940
if let Some(ref d) = v.disr_expr {
928941
self.s.space();
929942
self.word_space("=");

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
517517
}
518518

519519
fn check_variant(&mut self, cx: &LateContext<'_, '_>, v: &hir::Variant<'_>) {
520-
self.check_missing_docs_attrs(cx, Some(v.id), &v.attrs, v.span, "a", "variant");
520+
let span = cx.tcx.hir().span(v.id);
521+
self.check_missing_docs_attrs(cx, Some(v.id), &v.attrs, span, "a", "variant");
521522
}
522523
}
523524

src/librustc_lint/types.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,18 +1077,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
10771077
// We only warn if the largest variant is at least thrice as large as
10781078
// the second-largest.
10791079
if largest > slargest * 3 && slargest > 0 {
1080-
cx.struct_span_lint(
1081-
VARIANT_SIZE_DIFFERENCES,
1082-
enum_definition.variants[largest_index].span,
1083-
|lint| {
1084-
lint.build(&format!(
1085-
"enum variant is more than three times \
1080+
let span = cx.tcx.hir().span(enum_definition.variants[largest_index].id);
1081+
cx.struct_span_lint(VARIANT_SIZE_DIFFERENCES, span, |lint| {
1082+
lint.build(&format!(
1083+
"enum variant is more than three times \
10861084
larger ({} bytes) than the next largest",
1087-
largest
1088-
))
1089-
.emit()
1090-
},
1091-
);
1085+
largest
1086+
))
1087+
.emit()
1088+
});
10921089
}
10931090
}
10941091
}

src/librustc_middle/hir/map/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,11 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
525525
}
526526

527527
fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
528-
self.insert(v.span, v.id, Node::Variant(v));
528+
self.insert(DUMMY_SP, v.id, Node::Variant(v));
529529
self.with_parent(v.id, |this| {
530530
// Register the constructor of this variant.
531531
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
532-
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
532+
this.insert(DUMMY_SP, ctor_hir_id, Node::Ctor(&v.data));
533533
}
534534
intravisit::walk_variant(this, v, g, item_id);
535535
});

src/librustc_passes/dead.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
617617
id: hir::HirId,
618618
) {
619619
if self.should_warn_about_variant(&variant) {
620-
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
620+
let span = self.tcx.hir().span(variant.id);
621+
self.warn_dead_code(variant.id, span, variant.ident.name, "constructed");
621622
} else {
622623
intravisit::walk_variant(self, variant, g, id);
623624
}

src/librustc_passes/stability.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
305305
}
306306

307307
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
308-
self.annotate(var.id, &var.attrs, var.span, AnnotationKind::Required, |v| {
308+
let span = self.tcx.hir().span(var.id);
309+
self.annotate(var.id, &var.attrs, span, AnnotationKind::Required, |v| {
309310
if let Some(ctor_hir_id) = var.data.ctor_hir_id() {
310-
v.annotate(ctor_hir_id, &var.attrs, var.span, AnnotationKind::Required, |_| {});
311+
v.annotate(ctor_hir_id, &var.attrs, span, AnnotationKind::Required, |_| {});
311312
}
312313

313314
intravisit::walk_variant(v, var, g, item_id)
@@ -384,7 +385,8 @@ impl<'a, 'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'a, 'tcx> {
384385
}
385386

386387
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
387-
self.check_missing_stability(var.id, var.span);
388+
let span = self.tcx.hir().span(var.id);
389+
self.check_missing_stability(var.id, span);
388390
intravisit::walk_variant(self, var, g, item_id);
389391
}
390392

src/librustc_typeck/check/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,10 +2864,10 @@ pub fn check_enum<'tcx>(
28642864
Some(ref expr) => tcx.hir().span(expr.hir_id),
28652865
None => tcx.hir().span(variant_i_hir_id),
28662866
};
2867-
let span = match v.disr_expr {
2868-
Some(ref expr) => tcx.hir().span(expr.hir_id),
2869-
None => v.span,
2870-
};
2867+
let span = tcx.hir().span(match v.disr_expr {
2868+
Some(ref expr) => expr.hir_id,
2869+
None => v.id,
2870+
});
28712871
struct_span_err!(
28722872
tcx.sess,
28732873
span,

src/librustc_typeck/collect.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,9 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId, variants: &[hir::V
774774
} else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
775775
Some(discr)
776776
} else {
777-
struct_span_err!(tcx.sess, variant.span, E0370, "enum discriminant overflowed")
778-
.span_label(
779-
variant.span,
780-
format!("overflowed on value after {}", prev_discr.unwrap()),
781-
)
777+
let span = tcx.hir().span(variant.id);
778+
struct_span_err!(tcx.sess, span, E0370, "enum discriminant overflowed")
779+
.span_label(span, format!("overflowed on value after {}", prev_discr.unwrap()))
782780
.note(&format!(
783781
"explicitly set `{} = {}` if that is desired outcome",
784782
variant.ident, wrapped_discr

0 commit comments

Comments
 (0)