Skip to content

Commit e5e79f8

Browse files
committed
Auto merge of #144673 - Zalathar:rollup-0kpeq3n, r=Zalathar
Rollup of 6 pull requests Successful merges: - #144042 (Verify llvm-needs-components are not empty and match the --target value) - #144268 (Add method `find_ancestor_not_from_macro` and `find_ancestor_not_from_extern_macro` to supersede `find_oldest_ancestor_in_same_ctxt`) - #144411 (Remove `hello_world` directory) - #144662 (compiletest: Move directive names back into a separate file) - #144666 (Make sure to account for the right item universal regions in borrowck) - #144668 ([test][run-make] add needs-llvm-components) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 72716b1 + 3682d8c commit e5e79f8

File tree

58 files changed

+730
-439
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+730
-439
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
341341
}
342342
}
343343
} else if let LocalInfo::BlockTailTemp(info) = local_decl.local_info() {
344-
let sp = info.span.find_oldest_ancestor_in_same_ctxt();
344+
let sp = info.span.find_ancestor_not_from_macro().unwrap_or(info.span);
345345
if info.tail_result_is_ignored {
346346
// #85581: If the first mutable borrow's scope contains
347347
// the second borrow, this suggestion isn't helpful.

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -969,13 +969,28 @@ fn for_each_late_bound_region_in_item<'tcx>(
969969
mir_def_id: LocalDefId,
970970
mut f: impl FnMut(ty::Region<'tcx>),
971971
) {
972-
if !tcx.def_kind(mir_def_id).is_fn_like() {
973-
return;
974-
}
972+
let bound_vars = match tcx.def_kind(mir_def_id) {
973+
DefKind::Fn | DefKind::AssocFn => {
974+
tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id))
975+
}
976+
// We extract the bound vars from the deduced closure signature, since we may have
977+
// only deduced that a param in the closure signature is late-bound from a constraint
978+
// that we discover during typeck.
979+
DefKind::Closure => {
980+
let ty = tcx.type_of(mir_def_id).instantiate_identity();
981+
match *ty.kind() {
982+
ty::Closure(_, args) => args.as_closure().sig().bound_vars(),
983+
ty::CoroutineClosure(_, args) => {
984+
args.as_coroutine_closure().coroutine_closure_sig().bound_vars()
985+
}
986+
ty::Coroutine(_, _) | ty::Error(_) => return,
987+
_ => unreachable!("unexpected type for closure: {ty}"),
988+
}
989+
}
990+
_ => return,
991+
};
975992

976-
for (idx, bound_var) in
977-
tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id)).iter().enumerate()
978-
{
993+
for (idx, bound_var) in bound_vars.iter().enumerate() {
979994
if let ty::BoundVariableKind::Region(kind) = bound_var {
980995
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
981996
let liberated_region = ty::Region::new_late_param(tcx, mir_def_id.to_def_id(), kind);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13021302
None => ".clone()".to_string(),
13031303
};
13041304

1305-
let span = expr.span.find_oldest_ancestor_in_same_ctxt().shrink_to_hi();
1305+
let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span).shrink_to_hi();
13061306

13071307
diag.span_suggestion_verbose(
13081308
span,
@@ -1395,7 +1395,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13951395
.macro_backtrace()
13961396
.any(|x| matches!(x.kind, ExpnKind::Macro(MacroKind::Attr | MacroKind::Derive, ..)))
13971397
{
1398-
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
1398+
let span = expr
1399+
.span
1400+
.find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map())
1401+
.unwrap_or(expr.span);
13991402

14001403
let mut sugg = if self.precedence(expr) >= ExprPrecedence::Unambiguous {
14011404
vec![(span.shrink_to_hi(), ".into()".to_owned())]
@@ -2062,7 +2065,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20622065
None => sugg.to_string(),
20632066
};
20642067

2065-
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
2068+
let span = expr
2069+
.span
2070+
.find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map())
2071+
.unwrap_or(expr.span);
20662072
err.span_suggestion_verbose(span.shrink_to_hi(), msg, sugg, Applicability::HasPlaceholders);
20672073
true
20682074
}

compiler/rustc_lint/src/unused.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
185185
let mut op_warned = false;
186186

187187
if let Some(must_use_op) = must_use_op {
188-
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
188+
let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span);
189189
cx.emit_span_lint(
190190
UNUSED_MUST_USE,
191191
expr.span,
@@ -511,7 +511,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
511511
);
512512
}
513513
MustUsePath::Def(span, def_id, reason) => {
514-
let span = span.find_oldest_ancestor_in_same_ctxt();
514+
let span = span.find_ancestor_not_from_macro().unwrap_or(*span);
515515
cx.emit_span_lint(
516516
UNUSED_MUST_USE,
517517
span,

compiler/rustc_span/src/lib.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -716,12 +716,17 @@ impl Span {
716716
(!ctxt.is_root()).then(|| ctxt.outer_expn_data().call_site)
717717
}
718718

719-
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
719+
/// Find the first ancestor span that's contained within `outer`.
720720
///
721-
/// The span returned by this method may have a different [`SyntaxContext`] as `outer`.
721+
/// This method traverses the macro expansion ancestors until it finds the first span
722+
/// that's contained within `outer`.
723+
///
724+
/// The span returned by this method may have a different [`SyntaxContext`] than `outer`.
722725
/// If you need to extend the span, use [`find_ancestor_inside_same_ctxt`] instead,
723726
/// because joining spans with different syntax contexts can create unexpected results.
724727
///
728+
/// This is used to find the span of the macro call when a parent expr span, i.e. `outer`, is known.
729+
///
725730
/// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
726731
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
727732
while !outer.contains(self) {
@@ -730,8 +735,10 @@ impl Span {
730735
Some(self)
731736
}
732737

733-
/// Walk down the expansion ancestors to find a span with the same [`SyntaxContext`] as
734-
/// `other`.
738+
/// Find the first ancestor span with the same [`SyntaxContext`] as `other`.
739+
///
740+
/// This method traverses the macro expansion ancestors until it finds a span
741+
/// that has the same [`SyntaxContext`] as `other`.
735742
///
736743
/// Like [`find_ancestor_inside_same_ctxt`], but specifically for when spans might not
737744
/// overlap. Take care when using this, and prefer [`find_ancestor_inside`] or
@@ -747,9 +754,12 @@ impl Span {
747754
Some(self)
748755
}
749756

750-
/// Walk down the expansion ancestors to find a span that's contained within `outer` and
757+
/// Find the first ancestor span that's contained within `outer` and
751758
/// has the same [`SyntaxContext`] as `outer`.
752759
///
760+
/// This method traverses the macro expansion ancestors until it finds a span
761+
/// that is both contained within `outer` and has the same [`SyntaxContext`] as `outer`.
762+
///
753763
/// This method is the combination of [`find_ancestor_inside`] and
754764
/// [`find_ancestor_in_same_ctxt`] and should be preferred when extending the returned span.
755765
/// If you do not need to modify the span, use [`find_ancestor_inside`] instead.
@@ -763,43 +773,43 @@ impl Span {
763773
Some(self)
764774
}
765775

766-
/// Recursively walk down the expansion ancestors to find the oldest ancestor span with the same
767-
/// [`SyntaxContext`] the initial span.
776+
/// Find the first ancestor span that does not come from an external macro.
768777
///
769-
/// This method is suitable for peeling through *local* macro expansions to find the "innermost"
770-
/// span that is still local and shares the same [`SyntaxContext`]. For example, given
778+
/// This method traverses the macro expansion ancestors until it finds a span
779+
/// that is either from user-written code or from a local macro (defined in the current crate).
771780
///
772-
/// ```ignore (illustrative example, contains type error)
773-
/// macro_rules! outer {
774-
/// ($x: expr) => {
775-
/// inner!($x)
776-
/// }
777-
/// }
781+
/// External macros are those defined in dependencies or the standard library.
782+
/// This method is useful for reporting errors in user-controllable code and avoiding
783+
/// diagnostics inside external macros.
778784
///
779-
/// macro_rules! inner {
780-
/// ($x: expr) => {
781-
/// format!("error: {}", $x)
782-
/// //~^ ERROR mismatched types
783-
/// }
784-
/// }
785+
/// # See also
785786
///
786-
/// fn bar(x: &str) -> Result<(), Box<dyn std::error::Error>> {
787-
/// Err(outer!(x))
788-
/// }
789-
/// ```
787+
/// - [`Self::find_ancestor_not_from_macro`]
788+
/// - [`Self::in_external_macro`]
789+
pub fn find_ancestor_not_from_extern_macro(mut self, sm: &SourceMap) -> Option<Span> {
790+
while self.in_external_macro(sm) {
791+
self = self.parent_callsite()?;
792+
}
793+
Some(self)
794+
}
795+
796+
/// Find the first ancestor span that does not come from any macro expansion.
790797
///
791-
/// if provided the initial span of `outer!(x)` inside `bar`, this method will recurse
792-
/// the parent callsites until we reach `format!("error: {}", $x)`, at which point it is the
793-
/// oldest ancestor span that is both still local and shares the same [`SyntaxContext`] as the
794-
/// initial span.
795-
pub fn find_oldest_ancestor_in_same_ctxt(self) -> Span {
796-
let mut cur = self;
797-
while cur.eq_ctxt(self)
798-
&& let Some(parent_callsite) = cur.parent_callsite()
799-
{
800-
cur = parent_callsite;
798+
/// This method traverses the macro expansion ancestors until it finds a span
799+
/// that originates from user-written code rather than any macro-generated code.
800+
///
801+
/// This method is useful for reporting errors at the exact location users wrote code
802+
/// and providing suggestions at directly editable locations.
803+
///
804+
/// # See also
805+
///
806+
/// - [`Self::find_ancestor_not_from_extern_macro`]
807+
/// - [`Span::from_expansion`]
808+
pub fn find_ancestor_not_from_macro(mut self) -> Option<Span> {
809+
while self.from_expansion() {
810+
self = self.parent_callsite()?;
801811
}
802-
cur
812+
Some(self)
803813
}
804814

805815
/// Edition of the crate from which this span came.

src/doc/rustc-dev-guide/src/tests/directives.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,6 @@ See [Pretty-printer](compiletest.md#pretty-printer-tests).
293293

294294
- `no-auto-check-cfg` — disable auto check-cfg (only for `--check-cfg` tests)
295295
- [`revisions`](compiletest.md#revisions) — compile multiple times
296-
- [`unused-revision-names`](compiletest.md#ignoring-unused-revision-names) -
297-
suppress tidy checks for mentioning unknown revision names
298296
-[`forbid-output`](compiletest.md#incremental-tests) — incremental cfail rejects
299297
output pattern
300298
- [`should-ice`](compiletest.md#incremental-tests) — incremental cfail should
@@ -315,6 +313,17 @@ test suites that use those tools:
315313
- `llvm-cov-flags` adds extra flags when running LLVM's `llvm-cov` tool.
316314
- Used by [coverage tests](compiletest.md#coverage-tests) in `coverage-run` mode.
317315

316+
### Tidy specific directives
317+
318+
The following directives control how the [tidy script](../conventions.md#formatting)
319+
verifies tests.
320+
321+
- `ignore-tidy-target-specific-tests` disables checking that the appropriate
322+
LLVM component is required (via a `needs-llvm-components` directive) when a
323+
test is compiled for a specific target (via the `--target` flag in a
324+
`compile-flag` directive).
325+
- [`unused-revision-names`](compiletest.md#ignoring-unused-revision-names) -
326+
suppress tidy checks for mentioning unknown revision names.
318327

319328
## Substitutions
320329

0 commit comments

Comments
 (0)