Skip to content

Commit a759013

Browse files
committed
thread through a HirId to emit lints in the right place
Previously the lint would be reported at the right span, but could not be `allow`ed or `expect`ed in that location, because the lint was actually emitted using `CRATE_HIR_ID`
1 parent dfb6807 commit a759013

14 files changed

Lines changed: 65 additions & 29 deletions

File tree

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
346346
match infer {
347347
ty::TyVar(_) => self.next_ty_var(DUMMY_SP),
348348
ty::IntVar(_) => self.next_int_var(),
349-
ty::FloatVar(_) => self.next_float_var(DUMMY_SP),
349+
ty::FloatVar(_) => self.next_float_var(DUMMY_SP, None),
350350
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
351351
bug!("unexpected fresh ty outside of the trait solver")
352352
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
349349

350350
let tcx = self.tcx;
351351
match expr.kind {
352-
ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expected),
352+
ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expr.hir_id, expected),
353353
ExprKind::Binary(op, lhs, rhs) => self.check_expr_binop(expr, op, lhs, rhs, expected),
354354
ExprKind::Assign(lhs, rhs, span) => {
355355
self.check_expr_assign(expr, expected, lhs, rhs, span)

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
161161
.flat_map(|ty| ty.float_vid())
162162
.filter(|vid| roots.contains(&self.root_float_var(*vid)))
163163
.inspect(|vid| {
164-
let span = self.float_var_origin(*vid);
164+
let origin = self.float_var_origin(*vid);
165165
// Show the entire literal in the suggestion to make it clearer.
166-
let literal = self.tcx.sess.source_map().span_to_snippet(span).ok();
166+
let literal = self.tcx.sess.source_map().span_to_snippet(origin.span).ok();
167167
self.tcx.emit_node_span_lint(
168168
FLOAT_LITERAL_F32_FALLBACK,
169-
CRATE_HIR_ID,
170-
span,
169+
origin.lint_id.unwrap_or(CRATE_HIR_ID),
170+
origin.span,
171171
errors::FloatLiteralF32Fallback {
172-
span: literal.as_ref().map(|_| span),
172+
span: literal.as_ref().map(|_| origin.span),
173173
literal: literal.unwrap_or_default(),
174174
},
175175
);

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
718718
pub(in super::super) fn check_expr_lit(
719719
&self,
720720
lit: &hir::Lit,
721+
lint_id: HirId,
721722
expected: Expectation<'tcx>,
722723
) -> Ty<'tcx> {
723724
let tcx = self.tcx;
@@ -765,7 +766,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
765766
ty::Float(_) => Some(ty),
766767
_ => None,
767768
});
768-
opt_ty.unwrap_or_else(|| self.next_float_var(lit.span))
769+
opt_ty.unwrap_or_else(|| self.next_float_var(lit.span, Some(lint_id)))
769770
}
770771
ast::LitKind::Bool(_) => tcx.types.bool,
771772
ast::LitKind::CStr(_, _) => Ty::new_imm_ref(

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
917917
fn check_pat_expr_unadjusted(&self, lt: &'tcx hir::PatExpr<'tcx>) -> Ty<'tcx> {
918918
let ty = match &lt.kind {
919919
rustc_hir::PatExprKind::Lit { lit, negated } => {
920-
let ty = self.check_expr_lit(lit, Expectation::NoExpectation);
920+
let ty = self.check_expr_lit(lit, lt.hir_id, Expectation::NoExpectation);
921921
if *negated {
922922
self.register_bound(
923923
ty,

compiler/rustc_infer/src/infer/canonical/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ impl<'tcx> InferCtxt<'tcx> {
107107

108108
CanonicalVarKind::Int => self.next_int_var().into(),
109109

110-
CanonicalVarKind::Float => self.next_float_var(span).into(),
110+
CanonicalVarKind::Float => {
111+
// There is no HirId available to pass as a lint_id.
112+
self.next_float_var(span, None).into()
113+
}
111114

112115
CanonicalVarKind::PlaceholderTy(ty::PlaceholderType { universe, bound, .. }) => {
113116
let universe_mapped = universe_map(universe);

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1616
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
1717
use rustc_data_structures::unify as ut;
1818
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
19-
use rustc_hir as hir;
2019
use rustc_hir::def_id::{DefId, LocalDefId};
20+
use rustc_hir::{self as hir, HirId};
2121
use rustc_index::IndexVec;
2222
use rustc_macros::extension;
2323
pub use rustc_macros::{TypeFoldable, TypeVisitable};
@@ -39,6 +39,7 @@ use tracing::{debug, instrument};
3939
use type_variable::TypeVariableOrigin;
4040

4141
use crate::infer::snapshot::undo_log::UndoLog;
42+
use crate::infer::type_variable::FloatVariableOrigin;
4243
use crate::infer::unify_key::{ConstVariableOrigin, ConstVariableValue, ConstVidKey};
4344
use crate::traits::{
4445
self, ObligationCause, ObligationInspector, PredicateObligation, PredicateObligations,
@@ -109,9 +110,10 @@ pub struct InferCtxtInner<'tcx> {
109110
/// Map from floating variable to the kind of float it represents.
110111
float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
111112

112-
/// Map from floating variable to the origin span it came from. This is only used for the FCW
113-
/// for the fallback to `f32`, so can be removed once the `f32` fallback is removed.
114-
float_origin_span_storage: IndexVec<FloatVid, Span>,
113+
/// Map from floating variable to the origin span it came from, and the HirId that should be
114+
/// used to lint at that location. This is only used for the FCW for the fallback to `f32`,
115+
/// so can be removed once the `f32` fallback is removed.
116+
float_origin_origin_storage: IndexVec<FloatVid, FloatVariableOrigin>,
115117

116118
/// Tracks the set of region variables and the constraints between them.
117119
///
@@ -166,7 +168,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
166168
const_unification_storage: Default::default(),
167169
int_unification_storage: Default::default(),
168170
float_unification_storage: Default::default(),
169-
float_origin_span_storage: Default::default(),
171+
float_origin_origin_storage: Default::default(),
170172
region_constraint_storage: Some(Default::default()),
171173
region_obligations: Default::default(),
172174
region_assumptions: Default::default(),
@@ -653,8 +655,8 @@ impl<'tcx> InferCtxt<'tcx> {
653655
/// Returns the origin of the float type variable identified by `vid`.
654656
///
655657
/// No attempt is made to resolve `vid` to its root variable.
656-
pub fn float_var_origin(&self, vid: FloatVid) -> Span {
657-
self.inner.borrow_mut().float_origin_span_storage[vid]
658+
pub fn float_var_origin(&self, vid: FloatVid) -> FloatVariableOrigin {
659+
self.inner.borrow_mut().float_origin_origin_storage[vid]
658660
}
659661

660662
/// Returns the origin of the const variable identified by `vid`
@@ -834,10 +836,11 @@ impl<'tcx> InferCtxt<'tcx> {
834836
Ty::new_int_var(self.tcx, next_int_var_id)
835837
}
836838

837-
pub fn next_float_var(&self, span: Span) -> Ty<'tcx> {
839+
pub fn next_float_var(&self, span: Span, lint_id: Option<HirId>) -> Ty<'tcx> {
838840
let mut inner = self.inner.borrow_mut();
839841
let next_float_var_id = inner.float_unification_table().new_key(ty::FloatVarValue::Unknown);
840-
let span_index = inner.float_origin_span_storage.push(span);
842+
let origin = FloatVariableOrigin { span, lint_id };
843+
let span_index = inner.float_origin_origin_storage.push(origin);
841844
debug_assert_eq!(next_float_var_id, span_index);
842845
Ty::new_float_var(self.tcx, next_float_var_id)
843846
}

compiler/rustc_infer/src/infer/snapshot/fudge.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use rustc_middle::ty::{
66
self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid, TypeFoldable, TypeFolder,
77
TypeSuperFoldable, TypeVisitableExt,
88
};
9-
use rustc_span::Span;
109
use tracing::instrument;
1110
use ut::UnifyKey;
1211

1312
use super::VariableLengths;
14-
use crate::infer::type_variable::TypeVariableOrigin;
13+
use crate::infer::type_variable::{FloatVariableOrigin, TypeVariableOrigin};
1514
use crate::infer::unify_key::{ConstVariableValue, ConstVidKey};
1615
use crate::infer::{
1716
ConstVariableOrigin, InferCtxt, InferCtxtInner, RegionVariableOrigin, UnificationTable,
@@ -31,9 +30,9 @@ where
3130
fn float_vars_since_snapshot(
3231
inner: &mut InferCtxtInner<'_>,
3332
snapshot_var_len: usize,
34-
) -> (Range<FloatVid>, Vec<Span>) {
33+
) -> (Range<FloatVid>, Vec<FloatVariableOrigin>) {
3534
let range = vars_since_snapshot(&inner.float_unification_table(), snapshot_var_len);
36-
(range.clone(), range.map(|index| inner.float_origin_span_storage[index]).collect())
35+
(range.clone(), range.map(|index| inner.float_origin_origin_storage[index]).collect())
3736
}
3837

3938
fn const_vars_since_snapshot<'tcx>(
@@ -141,7 +140,7 @@ struct SnapshotVarData<'tcx> {
141140
region_vars: (Range<RegionVid>, Vec<RegionVariableOrigin<'tcx>>),
142141
type_vars: (Range<TyVid>, Vec<TypeVariableOrigin>),
143142
int_vars: Range<IntVid>,
144-
float_vars: (Range<FloatVid>, Vec<Span>),
143+
float_vars: (Range<FloatVid>, Vec<FloatVariableOrigin>),
145144
const_vars: (Range<ConstVid>, Vec<ConstVariableOrigin>),
146145
}
147146

@@ -215,8 +214,9 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
215214
ty::FloatVar(vid) => {
216215
if self.snapshot_vars.float_vars.0.contains(&vid) {
217216
let idx = vid.as_usize() - self.snapshot_vars.float_vars.0.start.as_usize();
218-
let span = self.snapshot_vars.float_vars.1[idx];
219-
self.infcx.next_float_var(span)
217+
let FloatVariableOrigin { span, lint_id } =
218+
self.snapshot_vars.float_vars.1[idx];
219+
self.infcx.next_float_var(span, lint_id)
220220
} else {
221221
ty
222222
}

compiler/rustc_infer/src/infer/type_variable.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ops::Range;
44

55
use rustc_data_structures::undo_log::Rollback;
66
use rustc_data_structures::{snapshot_vec as sv, unify as ut};
7+
use rustc_hir::HirId;
78
use rustc_hir::def_id::DefId;
89
use rustc_index::IndexVec;
910
use rustc_middle::bug;
@@ -99,6 +100,16 @@ pub struct TypeVariableOrigin {
99100
pub param_def_id: Option<DefId>,
100101
}
101102

103+
#[derive(Copy, Clone, Debug)]
104+
pub struct FloatVariableOrigin {
105+
pub span: Span,
106+
107+
/// `HirId` to lint at for this float variable, if any.
108+
///
109+
/// This should only be used for diagnostics.
110+
pub lint_id: Option<HirId>,
111+
}
112+
102113
#[derive(Clone)]
103114
pub(crate) struct TypeVariableData {
104115
origin: TypeVariableOrigin,

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ declare_lint_pass! {
4747
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
4848
EXPORTED_PRIVATE_DEPENDENCIES,
4949
FFI_UNWIND_CALLS,
50+
FLOAT_LITERAL_F32_FALLBACK,
5051
FORBIDDEN_LINT_GROUPS,
5152
FUNCTION_ITEM_REFERENCES,
5253
FUZZY_PROVENANCE_CASTS,

0 commit comments

Comments
 (0)