Skip to content

Commit d42105b

Browse files
committed
wip
1 parent 91d1a3f commit d42105b

File tree

7 files changed

+52
-72
lines changed

7 files changed

+52
-72
lines changed

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,10 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
618618
let last_lt = &self.gen_args.args[self.num_provided_lifetime_args() - 1];
619619
(self.tcx.mark_span_for_resize(last_lt.span()).shrink_to_hi(), false)
620620
};
621-
let path_sp = self.path_segment.ident.span.peel_ctxt();
621+
let path_sp = self.path_segment.ident.span;
622622
if !self.gen_args.args.iter().all(|arg| {
623623
arg.span().can_be_used_for_suggestions()
624-
&& arg.span().peel_ctxt().ctxt() == path_sp.ctxt()
624+
&& arg.span().peel_ctxt() == path_sp.peel_ctxt()
625625
}) || !path_sp.can_be_used_for_suggestions()
626626
{
627627
// Do not suggest syntax when macros are involved. (#90557)
@@ -670,10 +670,10 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
670670
);
671671
}
672672
AngleBrackets::Available => {
673-
let path_sp = self.path_segment.ident.span.peel_ctxt();
673+
let path_sp = self.path_segment.ident.span;
674674
if !self.gen_args.args.iter().all(|arg| {
675675
arg.span().can_be_used_for_suggestions()
676-
&& arg.span().peel_ctxt().ctxt() == path_sp.ctxt()
676+
&& arg.span().peel_ctxt() == path_sp.peel_ctxt()
677677
}) || !path_sp.can_be_used_for_suggestions()
678678
{
679679
// Do not suggest syntax when macros are involved. (#90557)

compiler/rustc_middle/src/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ pub fn struct_lint_level(
466466
/// This is used to test whether a lint should not even begin to figure out whether it should
467467
/// be reported on the current node.
468468
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
469-
let expn_data = span.peel_ctxt().ctxt().outer_expn_data();
469+
let expn_data = span.peel_ctxt().outer_expn_data();
470470
match expn_data.kind {
471471
ExpnKind::Inlined
472472
| ExpnKind::Root

compiler/rustc_middle/src/ty/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -2400,15 +2400,10 @@ impl<'tcx> TyCtxt<'tcx> {
24002400
/// `Span` before it was changed. This is used today only to be able to identify `Span`s coming
24012401
/// from a proc-macro even when it was modified, to avoid giving spurious suggestions when the
24022402
/// `Span` points at an attribute and not user code.
2403-
#[inline(always)]
24042403
pub fn mark_span_for_resize(self, span: Span) -> Span {
2405-
span
2406-
// if true {
2407-
// return span;
2408-
// }
2409-
// self.with_stable_hashing_context(|hcx| {
2410-
// span.mark_with_reason(None, rustc_span::DesugaringKind::Resize, span.edition(), hcx)
2411-
// })
2404+
self.with_stable_hashing_context(|hcx| {
2405+
span.mark_with_reason(None, rustc_span::DesugaringKind::Resize, span.edition(), hcx)
2406+
})
24122407
}
24132408

24142409
pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {

compiler/rustc_resolve/src/late/diagnostics.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
211211
suggestion: None,
212212
}
213213
} else {
214-
let item_span = path.last().unwrap().ident.span;
215-
let sp = item_span.peel_ctxt();
216-
let ctxt_kind = sp.ctxt().outer_expn_data().kind;
214+
let sp = path.last().unwrap().ident.span;
215+
let ctxt_kind = sp.peel_ctxt().outer_expn_data().kind;
217216
let (mod_prefix, mod_str, name, mod_label, suggestion) =
218217
if let ExpnKind::Macro(MacroKind::Attr | MacroKind::Bang, name) = ctxt_kind
219218
&& sp.parent_callsite().map(|p| (p.lo(), p.hi())) == Some((sp.lo(), sp.hi()))
@@ -260,7 +259,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
260259
};
261260

262261
Some((
263-
item_span.shrink_to_lo(),
262+
sp.shrink_to_lo(),
264263
match &item.kind {
265264
AssocItemKind::Fn(..) => "consider using the associated function",
266265
AssocItemKind::Const(..) => "consider using the associated constant",
@@ -304,28 +303,25 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
304303
("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
305304
} else {
306305
// check if we are in situation of typo like `True` instead of `true`.
307-
let override_suggestion =
308-
if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) {
309-
let item_typo = item_str.to_string().to_lowercase();
310-
Some((item_span, "you may want to use a bool value instead", item_typo))
311-
// FIXME(vincenzopalazzo): make the check smarter,
312-
// and maybe expand with levenshtein distance checks
313-
} else if item_str.as_str() == "printf" {
314-
Some((
315-
item_span,
316-
"you may have meant to use the `print` macro",
317-
"print!".to_owned(),
318-
))
319-
} else {
320-
suggestion
321-
};
306+
let override_suggestion = if ["true", "false"]
307+
.contains(&item_str.to_string().to_lowercase().as_str())
308+
{
309+
let item_typo = item_str.to_string().to_lowercase();
310+
Some((sp, "you may want to use a bool value instead", item_typo))
311+
// FIXME(vincenzopalazzo): make the check smarter,
312+
// and maybe expand with levenshtein distance checks
313+
} else if item_str.as_str() == "printf" {
314+
Some((sp, "you may have meant to use the `print` macro", "print!".to_owned()))
315+
} else {
316+
suggestion
317+
};
322318
(format!("{name}not found in {mod_label}"), override_suggestion)
323319
};
324320

325321
BaseError {
326322
msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
327323
fallback_label,
328-
span: item_span,
324+
span: sp,
329325
span_label: None,
330326
could_be_expr: false,
331327
suggestion,

compiler/rustc_span/src/lib.rs

+22-33
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ impl Span {
584584
/// Returns `true` if this span comes from any kind of macro, desugaring or inlining.
585585
#[inline]
586586
pub fn from_expansion(self) -> bool {
587-
self.peel_ctxt().ctxt() != SyntaxContext::root()
587+
self.peel_ctxt() != SyntaxContext::root()
588588
}
589589

590590
/// Returns `true` if `span` originates in a macro's expansion where debuginfo should be
@@ -769,7 +769,7 @@ impl Span {
769769

770770
/// Checks if this span arises from a compiler desugaring of kind `kind`.
771771
pub fn is_desugaring(self, kind: DesugaringKind) -> bool {
772-
match self.peel_ctxt().ctxt().outer_expn_data().kind {
772+
match self.peel_ctxt().outer_expn_data().kind {
773773
ExpnKind::Desugaring(k) => k == kind,
774774
_ => false,
775775
}
@@ -778,7 +778,7 @@ impl Span {
778778
/// Returns the compiler desugaring that created this span, or `None`
779779
/// if this span is not from a desugaring.
780780
pub fn desugaring_kind(self) -> Option<DesugaringKind> {
781-
match self.peel_ctxt().ctxt().outer_expn_data().kind {
781+
match self.peel_ctxt().outer_expn_data().kind {
782782
ExpnKind::Desugaring(k) => Some(k),
783783
_ => None,
784784
}
@@ -841,10 +841,10 @@ impl Span {
841841
// FIXME(jseyfried): `self.ctxt` should always equal `end.ctxt` here (cf. issue #23480).
842842
// Return the macro span on its own to avoid weird diagnostic output. It is preferable to
843843
// have an incomplete span than a completely nonsensical one.
844-
if self.peel_ctxt().ctxt() != end.peel_ctxt().ctxt() {
845-
if self.peel_ctxt().ctxt() == SyntaxContext::root() {
844+
if self.peel_ctxt() != end.peel_ctxt() {
845+
if self.peel_ctxt() == SyntaxContext::root() {
846846
return end;
847-
} else if end.peel_ctxt().ctxt() == SyntaxContext::root() {
847+
} else if end.peel_ctxt() == SyntaxContext::root() {
848848
return self;
849849
}
850850
// Both spans fall within a macro.
@@ -853,11 +853,7 @@ impl Span {
853853
Span::new(
854854
cmp::min(span_data.lo, end_data.lo),
855855
cmp::max(span_data.hi, end_data.hi),
856-
if self.peel_ctxt().ctxt() == SyntaxContext::root() {
857-
end_data.ctxt
858-
} else {
859-
span_data.ctxt
860-
},
856+
if self.peel_ctxt() == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
861857
if span_data.parent == end_data.parent { span_data.parent } else { None },
862858
)
863859
}
@@ -875,25 +871,22 @@ impl Span {
875871
Span::new(
876872
span_data.hi,
877873
end_data.lo,
878-
if end.peel_ctxt().ctxt() == SyntaxContext::root() {
879-
end_data.ctxt
880-
} else {
881-
span_data.ctxt
882-
},
874+
if end.peel_ctxt() == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
883875
if span_data.parent == end_data.parent { span_data.parent } else { None },
884876
)
885877
}
886878

887-
pub fn peel_ctxt(self) -> Span {
888-
// loop {
889-
// let data = self.data().ctxt.outer_expn_data();
890-
// if let ExpnKind::Desugaring(DesugaringKind::Resize) = data.kind {
891-
// self = data.call_site;
892-
// } else {
893-
// break;
894-
// }
895-
// }
896-
self
879+
pub fn peel_ctxt(self) -> SyntaxContext {
880+
let mut ctxt = self.ctxt();
881+
loop {
882+
let data = ctxt.outer_expn_data();
883+
if let ExpnKind::Desugaring(DesugaringKind::Resize) = data.kind {
884+
ctxt = data.call_site.ctxt();
885+
} else {
886+
break;
887+
}
888+
}
889+
ctxt
897890
}
898891

899892
/// Returns a `Span` from the beginning of `self` until the beginning of `end`.
@@ -915,9 +908,9 @@ impl Span {
915908
// Return the macro span on its own to avoid weird diagnostic output. It is preferable to
916909
// have an incomplete span than a completely nonsensical one.
917910
if span_data.ctxt != end_data.ctxt {
918-
if self.peel_ctxt().ctxt() == SyntaxContext::root() {
911+
if self.peel_ctxt() == SyntaxContext::root() {
919912
return end;
920-
} else if end.peel_ctxt().ctxt() == SyntaxContext::root() {
913+
} else if end.peel_ctxt() == SyntaxContext::root() {
921914
return self;
922915
}
923916
// Both spans fall within a macro.
@@ -926,11 +919,7 @@ impl Span {
926919
Span::new(
927920
span_data.lo,
928921
end_data.lo,
929-
if end.peel_ctxt().ctxt() == SyntaxContext::root() {
930-
end_data.ctxt
931-
} else {
932-
span_data.ctxt
933-
},
922+
if end.peel_ctxt() == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
934923
if span_data.parent == end_data.parent { span_data.parent } else { None },
935924
)
936925
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
956956
.join(", ");
957957

958958
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArgumentObligation { .. })
959-
&& match obligation.cause.span.peel_ctxt().ctxt().outer_expn_data().kind {
959+
&& match obligation.cause.span.peel_ctxt().outer_expn_data().kind {
960960
ExpnKind::Root
961961
| ExpnKind::AstPass(_)
962962
| ExpnKind::Desugaring(_)
@@ -1248,7 +1248,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
12481248
{
12491249
obligation.cause.code()
12501250
} else if let ExpnKind::Desugaring(DesugaringKind::ForLoop) =
1251-
span.peel_ctxt().ctxt().outer_expn_data().kind
1251+
span.peel_ctxt().outer_expn_data().kind
12521252
{
12531253
obligation.cause.code()
12541254
} else {
@@ -1323,7 +1323,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
13231323
// }
13241324
// ```
13251325
if !matches!(
1326-
span.peel_ctxt().ctxt().outer_expn_data().kind,
1326+
span.peel_ctxt().outer_expn_data().kind,
13271327
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
13281328
) {
13291329
return false;

src/tools/clippy/clippy_lints/src/ranges.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,11 @@ fn y_plus_one<'t>(cx: &LateContext<'_>, expr: &'t Expr<'_>) -> Option<Span> {
511511
rhs,
512512
) => {
513513
if is_integer_const(cx, lhs, 1)
514-
&& lhs.span.peel_ctxt().ctxt() == span.peel_ctxt().ctxt()
514+
&& lhs.span.peel_ctxt() == span.peel_ctxt()
515515
{
516516
Some(lhs.span.to(span))
517517
} else if is_integer_const(cx, rhs, 1)
518-
&& rhs.span.peel_ctxt().ctxt() == span.peel_ctxt().ctxt()
518+
&& rhs.span.peel_ctxt() == span.peel_ctxt()
519519
{
520520
Some(span.to(rhs.span))
521521
} else {
@@ -536,7 +536,7 @@ fn y_minus_one<'t>(cx: &LateContext<'_>, expr: &'t Expr<'_>) -> Option<Span> {
536536
_,
537537
rhs,
538538
) if is_integer_const(cx, rhs, 1)
539-
&& rhs.span.peel_ctxt().ctxt() == span.peel_ctxt().ctxt()
539+
&& rhs.span.peel_ctxt() == span.peel_ctxt()
540540
=> Some(span.to(rhs.span)),
541541
_ => None,
542542
}

0 commit comments

Comments
 (0)