Skip to content

Commit cfad637

Browse files
committed
Centralize calculation of FullfillmentError span
Use a method for caulculating deduplication and presentation span.
1 parent d333744 commit cfad637

File tree

8 files changed

+27
-33
lines changed

8 files changed

+27
-33
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
686686

687687
// Object safety violations or miscellaneous.
688688
Err(err) => {
689-
self.err_ctxt().report_selection_error(obligation.clone(), &obligation, &err);
689+
self.err_ctxt().report_selection_error(
690+
obligation.clone(),
691+
&obligation,
692+
&err,
693+
obligation.cause.span,
694+
);
690695
// Treat this like an obligation and follow through
691696
// with the unsizing - the lack of a coercion should
692697
// be silent, as it causes a type mismatch later.

compiler/rustc_infer/src/traits/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::traits::query::NoSolution;
1717
use rustc_middle::traits::solve::Certainty;
1818
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1919
use rustc_middle::ty::{self, Const, ToPredicate, Ty, TyCtxt};
20-
use rustc_span::Span;
20+
use rustc_span::{DesugaringKind, ExpnKind, Span};
2121

2222
pub use self::ImplSource::*;
2323
pub use self::SelectionError::*;
@@ -206,6 +206,18 @@ impl<'tcx> FulfillmentError<'tcx> {
206206
) -> FulfillmentError<'tcx> {
207207
FulfillmentError { obligation, code, root_obligation }
208208
}
209+
pub fn span(&self) -> Span {
210+
let mut span = self.obligation.cause.span;
211+
// We want to ignore desugarings here: spans are equivalent even
212+
// if one is the result of a desugaring and the other is not.
213+
let expn_data = span.ctxt().outer_expn_data();
214+
if let ExpnKind::Desugaring(desugaring) = expn_data.kind
215+
&& DesugaringKind::QuestionMark != desugaring
216+
{
217+
span = expn_data.call_site;
218+
}
219+
span
220+
}
209221
}
210222

211223
impl<'tcx> PolyTraitObligation<'tcx> {

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_session::config::DumpSolverProofTree;
4545
use rustc_session::Limit;
4646
use rustc_span::def_id::LOCAL_CRATE;
4747
use rustc_span::symbol::sym;
48-
use rustc_span::{BytePos, ExpnKind, Span, Symbol, DUMMY_SP};
48+
use rustc_span::{BytePos, Span, Symbol, DUMMY_SP};
4949
use std::borrow::Cow;
5050
use std::fmt;
5151
use std::iter;
@@ -124,15 +124,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
124124
});
125125

126126
for (index, error) in errors.iter().enumerate() {
127-
// We want to ignore desugarings here: spans are equivalent even
128-
// if one is the result of a desugaring and the other is not.
129-
let mut span = error.obligation.cause.span;
130-
let expn_data = span.ctxt().outer_expn_data();
131-
if let ExpnKind::Desugaring(_) = expn_data.kind {
132-
span = expn_data.call_site;
133-
}
134-
135-
error_map.entry(span).or_default().push(ErrorDescriptor {
127+
error_map.entry(error.span()).or_default().push(ErrorDescriptor {
136128
predicate: error.obligation.predicate,
137129
index: Some(index),
138130
});
@@ -176,16 +168,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
176168
if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion {
177169
let guar = self.report_fulfillment_error(error);
178170
reported = Some(guar);
179-
// We want to ignore desugarings here: spans are equivalent even
180-
// if one is the result of a desugaring and the other is not.
181-
let mut span = error.obligation.cause.span;
182-
let expn_data = span.ctxt().outer_expn_data();
183-
if let ExpnKind::Desugaring(_) = expn_data.kind {
184-
span = expn_data.call_site;
185-
}
186171
self.reported_trait_errors
187172
.borrow_mut()
188-
.entry(span)
173+
.entry(error.span())
189174
.or_insert_with(|| (vec![], guar))
190175
.0
191176
.push(error.obligation.predicate);
@@ -362,6 +347,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
362347
mut obligation: PredicateObligation<'tcx>,
363348
root_obligation: &PredicateObligation<'tcx>,
364349
error: &SelectionError<'tcx>,
350+
mut span: Span,
365351
) -> ErrorGuaranteed {
366352
let tcx = self.tcx;
367353

@@ -371,8 +357,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
371357
dump_proof_tree(root_obligation, self.infcx);
372358
}
373359

374-
let mut span = obligation.cause.span;
375-
376360
let mut err = match *error {
377361
SelectionError::Unimplemented => {
378362
// If this obligation was generated as a result of well-formedness checking, see if we
@@ -1504,6 +1488,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15041488
error.obligation.clone(),
15051489
&error.root_obligation,
15061490
selection_error,
1491+
error.span(),
15071492
),
15081493
FulfillmentErrorCode::ProjectionError(ref e) => {
15091494
self.report_projection_error(&error.obligation, e)

src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_errors::{Applicability, Diag};
1313
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_session::declare_lint_pass;
16+
use rustc_span::edition::Edition;
1617
use rustc_span::source_map::Spanned;
1718
use rustc_span::{sym, Span};
1819

@@ -137,8 +138,7 @@ fn assert_len_expr<'hir>(
137138
if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr)
138139
&& let ExprKind::Unary(UnOp::Not, condition) = &cond.kind
139140
&& let ExprKind::Binary(bin_op, left, right) = &condition.kind
140-
141-
&& let Some((cmp, asserted_len, slice_len)) = len_comparison(*bin_op, left, right)
141+
&& let Some((cmp, asserted_len, slice_len)) = len_comparison(bin_op, left, right)
142142
&& let ExprKind::MethodCall(method, recv, ..) = &slice_len.kind
143143
&& cx.typeck_results().expr_ty_adjusted(recv).peel_refs().is_slice()
144144
&& method.ident.name == sym::len

tests/ui/coroutine/issue-88653.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ fn foo(bar: bool) -> impl Coroutine<(bool,)> {
99
//~^ ERROR: type mismatch in coroutine arguments [E0631]
1010
//~| NOTE: expected due to this
1111
//~| NOTE: expected coroutine signature `fn((bool,)) -> _`
12-
//~| NOTE: in this expansion of desugaring of `impl Trait`
13-
//~| NOTE: in this expansion of desugaring of `impl Trait`
1412
|bar| {
1513
//~^ NOTE: found signature defined here
1614
if bar {

tests/ui/for/for-c-in-str.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ fn main() {
66
//~| NOTE `&str` is not an iterator
77
//~| HELP the trait `Iterator` is not implemented for `&str`
88
//~| NOTE required for `&str` to implement `IntoIterator`
9-
//~| NOTE in this expansion of desugaring of `for` loop
10-
//~| NOTE in this expansion of desugaring of `for` loop
11-
//~| NOTE in this expansion of desugaring of `for` loop
129
println!();
1310
}
1411
}

tests/ui/iterators/float_iterator_hint.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ fn main() {
44
for i in 0.2 {
55
//~^ ERROR `{float}` is not an iterator
66
//~| `{float}` is not an iterator
7-
//~| NOTE in this expansion of desugaring of `for` loop
8-
//~| NOTE in this expansion of desugaring of `for` loop
9-
//~| NOTE in this expansion of desugaring of `for` loop
10-
//~| NOTE in this expansion of desugaring of `for` loop
117
//~| NOTE if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end`
128
//~| NOTE required for `{float}` to implement `IntoIterator`
139
println!();

tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a
3333
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
3434
|
3535
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
36+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3637

3738
error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
3839
--> $DIR/issue-76168-hr-outlives-3.rs:6:1

0 commit comments

Comments
 (0)