From e68edb89ad1b34a3112c1cddd5541917743ccecd Mon Sep 17 00:00:00 2001 From: Vitaliy Busko Date: Thu, 19 Oct 2023 04:24:35 +0700 Subject: [PATCH 01/21] refactor(compiler/resolve): simplify some code Removes unnecessary allocates and double-sorting the same vector, makes the code a little nicer. --- compiler/rustc_resolve/src/check_unused.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 7dbbd4c34ea7d..6b39d7907f140 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -338,7 +338,7 @@ impl Resolver<'_, '_> { for unused in visitor.unused_imports.values() { let mut fixes = Vec::new(); - let mut spans = match calc_unused_spans(unused, unused.use_tree, unused.use_tree_id) { + let spans = match calc_unused_spans(unused, unused.use_tree, unused.use_tree_id) { UnusedSpanResult::Used => continue, UnusedSpanResult::FlatUnused(span, remove) => { fixes.push((remove, String::new())); @@ -356,20 +356,19 @@ impl Resolver<'_, '_> { } }; - let len = spans.len(); - spans.sort(); - let ms = MultiSpan::from_spans(spans.clone()); - let mut span_snippets = spans + let ms = MultiSpan::from_spans(spans); + + let mut span_snippets = ms + .primary_spans() .iter() - .filter_map(|s| match tcx.sess.source_map().span_to_snippet(*s) { - Ok(s) => Some(format!("`{s}`")), - _ => None, - }) + .filter_map(|span| tcx.sess.source_map().span_to_snippet(*span).ok()) + .map(|s| format!("`{s}`")) .collect::>(); span_snippets.sort(); + let msg = format!( "unused import{}{}", - pluralize!(len), + pluralize!(ms.primary_spans().len()), if !span_snippets.is_empty() { format!(": {}", span_snippets.join(", ")) } else { @@ -379,7 +378,7 @@ impl Resolver<'_, '_> { let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span { "remove the whole `use` item" - } else if spans.len() > 1 { + } else if ms.primary_spans().len() > 1 { "remove the unused imports" } else { "remove the unused import" From 855444ec54d1a745c5c53375792957170244e7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 24 Oct 2023 21:27:05 +0000 Subject: [PATCH 02/21] mv tests --- tests/ui/parser/{ => issues}/issue-100197-mut-let.fixed | 0 tests/ui/parser/{ => issues}/issue-100197-mut-let.rs | 0 tests/ui/parser/{ => issues}/issue-100197-mut-let.stderr | 0 tests/ui/parser/{ => issues}/issue-101477-enum.fixed | 0 tests/ui/parser/{ => issues}/issue-101477-enum.rs | 0 tests/ui/parser/{ => issues}/issue-101477-enum.stderr | 0 tests/ui/parser/{ => issues}/issue-101477-let.fixed | 0 tests/ui/parser/{ => issues}/issue-101477-let.rs | 0 tests/ui/parser/{ => issues}/issue-101477-let.stderr | 0 tests/ui/parser/{ => issues}/issue-102806.rs | 0 tests/ui/parser/{ => issues}/issue-102806.stderr | 0 tests/ui/parser/{ => issues}/issue-103143.rs | 0 tests/ui/parser/{ => issues}/issue-103143.stderr | 0 tests/ui/parser/{ => issues}/issue-103381.fixed | 0 tests/ui/parser/{ => issues}/issue-103381.rs | 0 tests/ui/parser/{ => issues}/issue-103381.stderr | 0 tests/ui/parser/{ => issues}/issue-103425.rs | 0 tests/ui/parser/{ => issues}/issue-103425.stderr | 0 tests/ui/parser/{ => issues}/issue-103451.rs | 0 tests/ui/parser/{ => issues}/issue-103451.stderr | 0 tests/ui/parser/{ => issues}/issue-103748-ICE-wrong-braces.rs | 0 tests/ui/parser/{ => issues}/issue-103748-ICE-wrong-braces.stderr | 0 tests/ui/parser/{ => issues}/issue-104620.rs | 0 tests/ui/parser/{ => issues}/issue-104620.stderr | 0 tests/ui/parser/{ => issues}/issue-104867-inc-dec-2.rs | 0 tests/ui/parser/{ => issues}/issue-104867-inc-dec-2.stderr | 0 tests/ui/parser/{ => issues}/issue-104867-inc-dec.rs | 0 tests/ui/parser/{ => issues}/issue-104867-inc-dec.stderr | 0 tests/ui/parser/{ => issues}/issue-105366.fixed | 0 tests/ui/parser/{ => issues}/issue-105366.rs | 0 tests/ui/parser/{ => issues}/issue-105366.stderr | 0 tests/ui/parser/{ => issues}/issue-105634.rs | 0 tests/ui/parser/{ => issues}/issue-107705.rs | 0 tests/ui/parser/{ => issues}/issue-107705.stderr | 0 tests/ui/parser/{ => issues}/issue-108495-dec.rs | 0 tests/ui/parser/{ => issues}/issue-108495-dec.stderr | 0 tests/ui/parser/{ => issues}/issue-112188.fixed | 0 tests/ui/parser/{ => issues}/issue-112188.rs | 0 tests/ui/parser/{ => issues}/issue-112188.stderr | 0 tests/ui/parser/{ => issues}/issue-113342.rs | 0 tests/ui/parser/{ => issues}/issue-113342.stderr | 0 tests/ui/parser/{ => issues}/issue-17718-parse-const.rs | 0 tests/ui/parser/{ => issues}/issue-39616.rs | 0 tests/ui/parser/{ => issues}/issue-39616.stderr | 0 tests/ui/parser/{ => issues}/issue-49257.rs | 0 tests/ui/parser/{ => issues}/issue-49257.stderr | 0 tests/ui/parser/{ => issues}/issue-61858.rs | 0 tests/ui/parser/{ => issues}/issue-61858.stderr | 0 .../ui/parser/{ => issues}/issue-68091-unicode-ident-after-if.rs | 0 .../parser/{ => issues}/issue-68091-unicode-ident-after-if.stderr | 0 .../issue-68092-unicode-ident-after-incomplete-expr.rs | 0 .../issue-68092-unicode-ident-after-incomplete-expr.stderr | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-1.rs | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-1.stderr | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-2.rs | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-2.stderr | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-3.rs | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-3.stderr | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue.rs | 0 tests/ui/parser/{ => issues}/issue-68987-unmatch-issue.stderr | 0 tests/ui/parser/{ => issues}/issue-81804.rs | 0 tests/ui/parser/{ => issues}/issue-81804.stderr | 0 tests/ui/parser/{ => issues}/issue-81827.rs | 0 tests/ui/parser/{ => issues}/issue-81827.stderr | 0 tests/ui/parser/{ => issues}/issue-87694-duplicated-pub.rs | 0 tests/ui/parser/{ => issues}/issue-87694-duplicated-pub.stderr | 0 tests/ui/parser/{ => issues}/issue-87694-misplaced-pub.rs | 0 tests/ui/parser/{ => issues}/issue-87694-misplaced-pub.stderr | 0 tests/ui/parser/{ => issues}/issue-90728.rs | 0 tests/ui/parser/{ => issues}/issue-90728.stderr | 0 tests/ui/parser/{ => issues}/issue-91421.rs | 0 tests/ui/parser/{ => issues}/issue-91421.stderr | 0 .../{ => issues}/issue-99625-enum-struct-mutually-exclusive.fixed | 0 .../{ => issues}/issue-99625-enum-struct-mutually-exclusive.rs | 0 .../issue-99625-enum-struct-mutually-exclusive.stderr | 0 .../{ => issues}/issue-99910-const-let-mutually-exclusive.fixed | 0 .../{ => issues}/issue-99910-const-let-mutually-exclusive.rs | 0 .../{ => issues}/issue-99910-const-let-mutually-exclusive.stderr | 0 78 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/parser/{ => issues}/issue-100197-mut-let.fixed (100%) rename tests/ui/parser/{ => issues}/issue-100197-mut-let.rs (100%) rename tests/ui/parser/{ => issues}/issue-100197-mut-let.stderr (100%) rename tests/ui/parser/{ => issues}/issue-101477-enum.fixed (100%) rename tests/ui/parser/{ => issues}/issue-101477-enum.rs (100%) rename tests/ui/parser/{ => issues}/issue-101477-enum.stderr (100%) rename tests/ui/parser/{ => issues}/issue-101477-let.fixed (100%) rename tests/ui/parser/{ => issues}/issue-101477-let.rs (100%) rename tests/ui/parser/{ => issues}/issue-101477-let.stderr (100%) rename tests/ui/parser/{ => issues}/issue-102806.rs (100%) rename tests/ui/parser/{ => issues}/issue-102806.stderr (100%) rename tests/ui/parser/{ => issues}/issue-103143.rs (100%) rename tests/ui/parser/{ => issues}/issue-103143.stderr (100%) rename tests/ui/parser/{ => issues}/issue-103381.fixed (100%) rename tests/ui/parser/{ => issues}/issue-103381.rs (100%) rename tests/ui/parser/{ => issues}/issue-103381.stderr (100%) rename tests/ui/parser/{ => issues}/issue-103425.rs (100%) rename tests/ui/parser/{ => issues}/issue-103425.stderr (100%) rename tests/ui/parser/{ => issues}/issue-103451.rs (100%) rename tests/ui/parser/{ => issues}/issue-103451.stderr (100%) rename tests/ui/parser/{ => issues}/issue-103748-ICE-wrong-braces.rs (100%) rename tests/ui/parser/{ => issues}/issue-103748-ICE-wrong-braces.stderr (100%) rename tests/ui/parser/{ => issues}/issue-104620.rs (100%) rename tests/ui/parser/{ => issues}/issue-104620.stderr (100%) rename tests/ui/parser/{ => issues}/issue-104867-inc-dec-2.rs (100%) rename tests/ui/parser/{ => issues}/issue-104867-inc-dec-2.stderr (100%) rename tests/ui/parser/{ => issues}/issue-104867-inc-dec.rs (100%) rename tests/ui/parser/{ => issues}/issue-104867-inc-dec.stderr (100%) rename tests/ui/parser/{ => issues}/issue-105366.fixed (100%) rename tests/ui/parser/{ => issues}/issue-105366.rs (100%) rename tests/ui/parser/{ => issues}/issue-105366.stderr (100%) rename tests/ui/parser/{ => issues}/issue-105634.rs (100%) rename tests/ui/parser/{ => issues}/issue-107705.rs (100%) rename tests/ui/parser/{ => issues}/issue-107705.stderr (100%) rename tests/ui/parser/{ => issues}/issue-108495-dec.rs (100%) rename tests/ui/parser/{ => issues}/issue-108495-dec.stderr (100%) rename tests/ui/parser/{ => issues}/issue-112188.fixed (100%) rename tests/ui/parser/{ => issues}/issue-112188.rs (100%) rename tests/ui/parser/{ => issues}/issue-112188.stderr (100%) rename tests/ui/parser/{ => issues}/issue-113342.rs (100%) rename tests/ui/parser/{ => issues}/issue-113342.stderr (100%) rename tests/ui/parser/{ => issues}/issue-17718-parse-const.rs (100%) rename tests/ui/parser/{ => issues}/issue-39616.rs (100%) rename tests/ui/parser/{ => issues}/issue-39616.stderr (100%) rename tests/ui/parser/{ => issues}/issue-49257.rs (100%) rename tests/ui/parser/{ => issues}/issue-49257.stderr (100%) rename tests/ui/parser/{ => issues}/issue-61858.rs (100%) rename tests/ui/parser/{ => issues}/issue-61858.stderr (100%) rename tests/ui/parser/{ => issues}/issue-68091-unicode-ident-after-if.rs (100%) rename tests/ui/parser/{ => issues}/issue-68091-unicode-ident-after-if.stderr (100%) rename tests/ui/parser/{ => issues}/issue-68092-unicode-ident-after-incomplete-expr.rs (100%) rename tests/ui/parser/{ => issues}/issue-68092-unicode-ident-after-incomplete-expr.stderr (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-1.rs (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-1.stderr (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-2.rs (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-2.stderr (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-3.rs (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue-3.stderr (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue.rs (100%) rename tests/ui/parser/{ => issues}/issue-68987-unmatch-issue.stderr (100%) rename tests/ui/parser/{ => issues}/issue-81804.rs (100%) rename tests/ui/parser/{ => issues}/issue-81804.stderr (100%) rename tests/ui/parser/{ => issues}/issue-81827.rs (100%) rename tests/ui/parser/{ => issues}/issue-81827.stderr (100%) rename tests/ui/parser/{ => issues}/issue-87694-duplicated-pub.rs (100%) rename tests/ui/parser/{ => issues}/issue-87694-duplicated-pub.stderr (100%) rename tests/ui/parser/{ => issues}/issue-87694-misplaced-pub.rs (100%) rename tests/ui/parser/{ => issues}/issue-87694-misplaced-pub.stderr (100%) rename tests/ui/parser/{ => issues}/issue-90728.rs (100%) rename tests/ui/parser/{ => issues}/issue-90728.stderr (100%) rename tests/ui/parser/{ => issues}/issue-91421.rs (100%) rename tests/ui/parser/{ => issues}/issue-91421.stderr (100%) rename tests/ui/parser/{ => issues}/issue-99625-enum-struct-mutually-exclusive.fixed (100%) rename tests/ui/parser/{ => issues}/issue-99625-enum-struct-mutually-exclusive.rs (100%) rename tests/ui/parser/{ => issues}/issue-99625-enum-struct-mutually-exclusive.stderr (100%) rename tests/ui/parser/{ => issues}/issue-99910-const-let-mutually-exclusive.fixed (100%) rename tests/ui/parser/{ => issues}/issue-99910-const-let-mutually-exclusive.rs (100%) rename tests/ui/parser/{ => issues}/issue-99910-const-let-mutually-exclusive.stderr (100%) diff --git a/tests/ui/parser/issue-100197-mut-let.fixed b/tests/ui/parser/issues/issue-100197-mut-let.fixed similarity index 100% rename from tests/ui/parser/issue-100197-mut-let.fixed rename to tests/ui/parser/issues/issue-100197-mut-let.fixed diff --git a/tests/ui/parser/issue-100197-mut-let.rs b/tests/ui/parser/issues/issue-100197-mut-let.rs similarity index 100% rename from tests/ui/parser/issue-100197-mut-let.rs rename to tests/ui/parser/issues/issue-100197-mut-let.rs diff --git a/tests/ui/parser/issue-100197-mut-let.stderr b/tests/ui/parser/issues/issue-100197-mut-let.stderr similarity index 100% rename from tests/ui/parser/issue-100197-mut-let.stderr rename to tests/ui/parser/issues/issue-100197-mut-let.stderr diff --git a/tests/ui/parser/issue-101477-enum.fixed b/tests/ui/parser/issues/issue-101477-enum.fixed similarity index 100% rename from tests/ui/parser/issue-101477-enum.fixed rename to tests/ui/parser/issues/issue-101477-enum.fixed diff --git a/tests/ui/parser/issue-101477-enum.rs b/tests/ui/parser/issues/issue-101477-enum.rs similarity index 100% rename from tests/ui/parser/issue-101477-enum.rs rename to tests/ui/parser/issues/issue-101477-enum.rs diff --git a/tests/ui/parser/issue-101477-enum.stderr b/tests/ui/parser/issues/issue-101477-enum.stderr similarity index 100% rename from tests/ui/parser/issue-101477-enum.stderr rename to tests/ui/parser/issues/issue-101477-enum.stderr diff --git a/tests/ui/parser/issue-101477-let.fixed b/tests/ui/parser/issues/issue-101477-let.fixed similarity index 100% rename from tests/ui/parser/issue-101477-let.fixed rename to tests/ui/parser/issues/issue-101477-let.fixed diff --git a/tests/ui/parser/issue-101477-let.rs b/tests/ui/parser/issues/issue-101477-let.rs similarity index 100% rename from tests/ui/parser/issue-101477-let.rs rename to tests/ui/parser/issues/issue-101477-let.rs diff --git a/tests/ui/parser/issue-101477-let.stderr b/tests/ui/parser/issues/issue-101477-let.stderr similarity index 100% rename from tests/ui/parser/issue-101477-let.stderr rename to tests/ui/parser/issues/issue-101477-let.stderr diff --git a/tests/ui/parser/issue-102806.rs b/tests/ui/parser/issues/issue-102806.rs similarity index 100% rename from tests/ui/parser/issue-102806.rs rename to tests/ui/parser/issues/issue-102806.rs diff --git a/tests/ui/parser/issue-102806.stderr b/tests/ui/parser/issues/issue-102806.stderr similarity index 100% rename from tests/ui/parser/issue-102806.stderr rename to tests/ui/parser/issues/issue-102806.stderr diff --git a/tests/ui/parser/issue-103143.rs b/tests/ui/parser/issues/issue-103143.rs similarity index 100% rename from tests/ui/parser/issue-103143.rs rename to tests/ui/parser/issues/issue-103143.rs diff --git a/tests/ui/parser/issue-103143.stderr b/tests/ui/parser/issues/issue-103143.stderr similarity index 100% rename from tests/ui/parser/issue-103143.stderr rename to tests/ui/parser/issues/issue-103143.stderr diff --git a/tests/ui/parser/issue-103381.fixed b/tests/ui/parser/issues/issue-103381.fixed similarity index 100% rename from tests/ui/parser/issue-103381.fixed rename to tests/ui/parser/issues/issue-103381.fixed diff --git a/tests/ui/parser/issue-103381.rs b/tests/ui/parser/issues/issue-103381.rs similarity index 100% rename from tests/ui/parser/issue-103381.rs rename to tests/ui/parser/issues/issue-103381.rs diff --git a/tests/ui/parser/issue-103381.stderr b/tests/ui/parser/issues/issue-103381.stderr similarity index 100% rename from tests/ui/parser/issue-103381.stderr rename to tests/ui/parser/issues/issue-103381.stderr diff --git a/tests/ui/parser/issue-103425.rs b/tests/ui/parser/issues/issue-103425.rs similarity index 100% rename from tests/ui/parser/issue-103425.rs rename to tests/ui/parser/issues/issue-103425.rs diff --git a/tests/ui/parser/issue-103425.stderr b/tests/ui/parser/issues/issue-103425.stderr similarity index 100% rename from tests/ui/parser/issue-103425.stderr rename to tests/ui/parser/issues/issue-103425.stderr diff --git a/tests/ui/parser/issue-103451.rs b/tests/ui/parser/issues/issue-103451.rs similarity index 100% rename from tests/ui/parser/issue-103451.rs rename to tests/ui/parser/issues/issue-103451.rs diff --git a/tests/ui/parser/issue-103451.stderr b/tests/ui/parser/issues/issue-103451.stderr similarity index 100% rename from tests/ui/parser/issue-103451.stderr rename to tests/ui/parser/issues/issue-103451.stderr diff --git a/tests/ui/parser/issue-103748-ICE-wrong-braces.rs b/tests/ui/parser/issues/issue-103748-ICE-wrong-braces.rs similarity index 100% rename from tests/ui/parser/issue-103748-ICE-wrong-braces.rs rename to tests/ui/parser/issues/issue-103748-ICE-wrong-braces.rs diff --git a/tests/ui/parser/issue-103748-ICE-wrong-braces.stderr b/tests/ui/parser/issues/issue-103748-ICE-wrong-braces.stderr similarity index 100% rename from tests/ui/parser/issue-103748-ICE-wrong-braces.stderr rename to tests/ui/parser/issues/issue-103748-ICE-wrong-braces.stderr diff --git a/tests/ui/parser/issue-104620.rs b/tests/ui/parser/issues/issue-104620.rs similarity index 100% rename from tests/ui/parser/issue-104620.rs rename to tests/ui/parser/issues/issue-104620.rs diff --git a/tests/ui/parser/issue-104620.stderr b/tests/ui/parser/issues/issue-104620.stderr similarity index 100% rename from tests/ui/parser/issue-104620.stderr rename to tests/ui/parser/issues/issue-104620.stderr diff --git a/tests/ui/parser/issue-104867-inc-dec-2.rs b/tests/ui/parser/issues/issue-104867-inc-dec-2.rs similarity index 100% rename from tests/ui/parser/issue-104867-inc-dec-2.rs rename to tests/ui/parser/issues/issue-104867-inc-dec-2.rs diff --git a/tests/ui/parser/issue-104867-inc-dec-2.stderr b/tests/ui/parser/issues/issue-104867-inc-dec-2.stderr similarity index 100% rename from tests/ui/parser/issue-104867-inc-dec-2.stderr rename to tests/ui/parser/issues/issue-104867-inc-dec-2.stderr diff --git a/tests/ui/parser/issue-104867-inc-dec.rs b/tests/ui/parser/issues/issue-104867-inc-dec.rs similarity index 100% rename from tests/ui/parser/issue-104867-inc-dec.rs rename to tests/ui/parser/issues/issue-104867-inc-dec.rs diff --git a/tests/ui/parser/issue-104867-inc-dec.stderr b/tests/ui/parser/issues/issue-104867-inc-dec.stderr similarity index 100% rename from tests/ui/parser/issue-104867-inc-dec.stderr rename to tests/ui/parser/issues/issue-104867-inc-dec.stderr diff --git a/tests/ui/parser/issue-105366.fixed b/tests/ui/parser/issues/issue-105366.fixed similarity index 100% rename from tests/ui/parser/issue-105366.fixed rename to tests/ui/parser/issues/issue-105366.fixed diff --git a/tests/ui/parser/issue-105366.rs b/tests/ui/parser/issues/issue-105366.rs similarity index 100% rename from tests/ui/parser/issue-105366.rs rename to tests/ui/parser/issues/issue-105366.rs diff --git a/tests/ui/parser/issue-105366.stderr b/tests/ui/parser/issues/issue-105366.stderr similarity index 100% rename from tests/ui/parser/issue-105366.stderr rename to tests/ui/parser/issues/issue-105366.stderr diff --git a/tests/ui/parser/issue-105634.rs b/tests/ui/parser/issues/issue-105634.rs similarity index 100% rename from tests/ui/parser/issue-105634.rs rename to tests/ui/parser/issues/issue-105634.rs diff --git a/tests/ui/parser/issue-107705.rs b/tests/ui/parser/issues/issue-107705.rs similarity index 100% rename from tests/ui/parser/issue-107705.rs rename to tests/ui/parser/issues/issue-107705.rs diff --git a/tests/ui/parser/issue-107705.stderr b/tests/ui/parser/issues/issue-107705.stderr similarity index 100% rename from tests/ui/parser/issue-107705.stderr rename to tests/ui/parser/issues/issue-107705.stderr diff --git a/tests/ui/parser/issue-108495-dec.rs b/tests/ui/parser/issues/issue-108495-dec.rs similarity index 100% rename from tests/ui/parser/issue-108495-dec.rs rename to tests/ui/parser/issues/issue-108495-dec.rs diff --git a/tests/ui/parser/issue-108495-dec.stderr b/tests/ui/parser/issues/issue-108495-dec.stderr similarity index 100% rename from tests/ui/parser/issue-108495-dec.stderr rename to tests/ui/parser/issues/issue-108495-dec.stderr diff --git a/tests/ui/parser/issue-112188.fixed b/tests/ui/parser/issues/issue-112188.fixed similarity index 100% rename from tests/ui/parser/issue-112188.fixed rename to tests/ui/parser/issues/issue-112188.fixed diff --git a/tests/ui/parser/issue-112188.rs b/tests/ui/parser/issues/issue-112188.rs similarity index 100% rename from tests/ui/parser/issue-112188.rs rename to tests/ui/parser/issues/issue-112188.rs diff --git a/tests/ui/parser/issue-112188.stderr b/tests/ui/parser/issues/issue-112188.stderr similarity index 100% rename from tests/ui/parser/issue-112188.stderr rename to tests/ui/parser/issues/issue-112188.stderr diff --git a/tests/ui/parser/issue-113342.rs b/tests/ui/parser/issues/issue-113342.rs similarity index 100% rename from tests/ui/parser/issue-113342.rs rename to tests/ui/parser/issues/issue-113342.rs diff --git a/tests/ui/parser/issue-113342.stderr b/tests/ui/parser/issues/issue-113342.stderr similarity index 100% rename from tests/ui/parser/issue-113342.stderr rename to tests/ui/parser/issues/issue-113342.stderr diff --git a/tests/ui/parser/issue-17718-parse-const.rs b/tests/ui/parser/issues/issue-17718-parse-const.rs similarity index 100% rename from tests/ui/parser/issue-17718-parse-const.rs rename to tests/ui/parser/issues/issue-17718-parse-const.rs diff --git a/tests/ui/parser/issue-39616.rs b/tests/ui/parser/issues/issue-39616.rs similarity index 100% rename from tests/ui/parser/issue-39616.rs rename to tests/ui/parser/issues/issue-39616.rs diff --git a/tests/ui/parser/issue-39616.stderr b/tests/ui/parser/issues/issue-39616.stderr similarity index 100% rename from tests/ui/parser/issue-39616.stderr rename to tests/ui/parser/issues/issue-39616.stderr diff --git a/tests/ui/parser/issue-49257.rs b/tests/ui/parser/issues/issue-49257.rs similarity index 100% rename from tests/ui/parser/issue-49257.rs rename to tests/ui/parser/issues/issue-49257.rs diff --git a/tests/ui/parser/issue-49257.stderr b/tests/ui/parser/issues/issue-49257.stderr similarity index 100% rename from tests/ui/parser/issue-49257.stderr rename to tests/ui/parser/issues/issue-49257.stderr diff --git a/tests/ui/parser/issue-61858.rs b/tests/ui/parser/issues/issue-61858.rs similarity index 100% rename from tests/ui/parser/issue-61858.rs rename to tests/ui/parser/issues/issue-61858.rs diff --git a/tests/ui/parser/issue-61858.stderr b/tests/ui/parser/issues/issue-61858.stderr similarity index 100% rename from tests/ui/parser/issue-61858.stderr rename to tests/ui/parser/issues/issue-61858.stderr diff --git a/tests/ui/parser/issue-68091-unicode-ident-after-if.rs b/tests/ui/parser/issues/issue-68091-unicode-ident-after-if.rs similarity index 100% rename from tests/ui/parser/issue-68091-unicode-ident-after-if.rs rename to tests/ui/parser/issues/issue-68091-unicode-ident-after-if.rs diff --git a/tests/ui/parser/issue-68091-unicode-ident-after-if.stderr b/tests/ui/parser/issues/issue-68091-unicode-ident-after-if.stderr similarity index 100% rename from tests/ui/parser/issue-68091-unicode-ident-after-if.stderr rename to tests/ui/parser/issues/issue-68091-unicode-ident-after-if.stderr diff --git a/tests/ui/parser/issue-68092-unicode-ident-after-incomplete-expr.rs b/tests/ui/parser/issues/issue-68092-unicode-ident-after-incomplete-expr.rs similarity index 100% rename from tests/ui/parser/issue-68092-unicode-ident-after-incomplete-expr.rs rename to tests/ui/parser/issues/issue-68092-unicode-ident-after-incomplete-expr.rs diff --git a/tests/ui/parser/issue-68092-unicode-ident-after-incomplete-expr.stderr b/tests/ui/parser/issues/issue-68092-unicode-ident-after-incomplete-expr.stderr similarity index 100% rename from tests/ui/parser/issue-68092-unicode-ident-after-incomplete-expr.stderr rename to tests/ui/parser/issues/issue-68092-unicode-ident-after-incomplete-expr.stderr diff --git a/tests/ui/parser/issue-68987-unmatch-issue-1.rs b/tests/ui/parser/issues/issue-68987-unmatch-issue-1.rs similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue-1.rs rename to tests/ui/parser/issues/issue-68987-unmatch-issue-1.rs diff --git a/tests/ui/parser/issue-68987-unmatch-issue-1.stderr b/tests/ui/parser/issues/issue-68987-unmatch-issue-1.stderr similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue-1.stderr rename to tests/ui/parser/issues/issue-68987-unmatch-issue-1.stderr diff --git a/tests/ui/parser/issue-68987-unmatch-issue-2.rs b/tests/ui/parser/issues/issue-68987-unmatch-issue-2.rs similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue-2.rs rename to tests/ui/parser/issues/issue-68987-unmatch-issue-2.rs diff --git a/tests/ui/parser/issue-68987-unmatch-issue-2.stderr b/tests/ui/parser/issues/issue-68987-unmatch-issue-2.stderr similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue-2.stderr rename to tests/ui/parser/issues/issue-68987-unmatch-issue-2.stderr diff --git a/tests/ui/parser/issue-68987-unmatch-issue-3.rs b/tests/ui/parser/issues/issue-68987-unmatch-issue-3.rs similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue-3.rs rename to tests/ui/parser/issues/issue-68987-unmatch-issue-3.rs diff --git a/tests/ui/parser/issue-68987-unmatch-issue-3.stderr b/tests/ui/parser/issues/issue-68987-unmatch-issue-3.stderr similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue-3.stderr rename to tests/ui/parser/issues/issue-68987-unmatch-issue-3.stderr diff --git a/tests/ui/parser/issue-68987-unmatch-issue.rs b/tests/ui/parser/issues/issue-68987-unmatch-issue.rs similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue.rs rename to tests/ui/parser/issues/issue-68987-unmatch-issue.rs diff --git a/tests/ui/parser/issue-68987-unmatch-issue.stderr b/tests/ui/parser/issues/issue-68987-unmatch-issue.stderr similarity index 100% rename from tests/ui/parser/issue-68987-unmatch-issue.stderr rename to tests/ui/parser/issues/issue-68987-unmatch-issue.stderr diff --git a/tests/ui/parser/issue-81804.rs b/tests/ui/parser/issues/issue-81804.rs similarity index 100% rename from tests/ui/parser/issue-81804.rs rename to tests/ui/parser/issues/issue-81804.rs diff --git a/tests/ui/parser/issue-81804.stderr b/tests/ui/parser/issues/issue-81804.stderr similarity index 100% rename from tests/ui/parser/issue-81804.stderr rename to tests/ui/parser/issues/issue-81804.stderr diff --git a/tests/ui/parser/issue-81827.rs b/tests/ui/parser/issues/issue-81827.rs similarity index 100% rename from tests/ui/parser/issue-81827.rs rename to tests/ui/parser/issues/issue-81827.rs diff --git a/tests/ui/parser/issue-81827.stderr b/tests/ui/parser/issues/issue-81827.stderr similarity index 100% rename from tests/ui/parser/issue-81827.stderr rename to tests/ui/parser/issues/issue-81827.stderr diff --git a/tests/ui/parser/issue-87694-duplicated-pub.rs b/tests/ui/parser/issues/issue-87694-duplicated-pub.rs similarity index 100% rename from tests/ui/parser/issue-87694-duplicated-pub.rs rename to tests/ui/parser/issues/issue-87694-duplicated-pub.rs diff --git a/tests/ui/parser/issue-87694-duplicated-pub.stderr b/tests/ui/parser/issues/issue-87694-duplicated-pub.stderr similarity index 100% rename from tests/ui/parser/issue-87694-duplicated-pub.stderr rename to tests/ui/parser/issues/issue-87694-duplicated-pub.stderr diff --git a/tests/ui/parser/issue-87694-misplaced-pub.rs b/tests/ui/parser/issues/issue-87694-misplaced-pub.rs similarity index 100% rename from tests/ui/parser/issue-87694-misplaced-pub.rs rename to tests/ui/parser/issues/issue-87694-misplaced-pub.rs diff --git a/tests/ui/parser/issue-87694-misplaced-pub.stderr b/tests/ui/parser/issues/issue-87694-misplaced-pub.stderr similarity index 100% rename from tests/ui/parser/issue-87694-misplaced-pub.stderr rename to tests/ui/parser/issues/issue-87694-misplaced-pub.stderr diff --git a/tests/ui/parser/issue-90728.rs b/tests/ui/parser/issues/issue-90728.rs similarity index 100% rename from tests/ui/parser/issue-90728.rs rename to tests/ui/parser/issues/issue-90728.rs diff --git a/tests/ui/parser/issue-90728.stderr b/tests/ui/parser/issues/issue-90728.stderr similarity index 100% rename from tests/ui/parser/issue-90728.stderr rename to tests/ui/parser/issues/issue-90728.stderr diff --git a/tests/ui/parser/issue-91421.rs b/tests/ui/parser/issues/issue-91421.rs similarity index 100% rename from tests/ui/parser/issue-91421.rs rename to tests/ui/parser/issues/issue-91421.rs diff --git a/tests/ui/parser/issue-91421.stderr b/tests/ui/parser/issues/issue-91421.stderr similarity index 100% rename from tests/ui/parser/issue-91421.stderr rename to tests/ui/parser/issues/issue-91421.stderr diff --git a/tests/ui/parser/issue-99625-enum-struct-mutually-exclusive.fixed b/tests/ui/parser/issues/issue-99625-enum-struct-mutually-exclusive.fixed similarity index 100% rename from tests/ui/parser/issue-99625-enum-struct-mutually-exclusive.fixed rename to tests/ui/parser/issues/issue-99625-enum-struct-mutually-exclusive.fixed diff --git a/tests/ui/parser/issue-99625-enum-struct-mutually-exclusive.rs b/tests/ui/parser/issues/issue-99625-enum-struct-mutually-exclusive.rs similarity index 100% rename from tests/ui/parser/issue-99625-enum-struct-mutually-exclusive.rs rename to tests/ui/parser/issues/issue-99625-enum-struct-mutually-exclusive.rs diff --git a/tests/ui/parser/issue-99625-enum-struct-mutually-exclusive.stderr b/tests/ui/parser/issues/issue-99625-enum-struct-mutually-exclusive.stderr similarity index 100% rename from tests/ui/parser/issue-99625-enum-struct-mutually-exclusive.stderr rename to tests/ui/parser/issues/issue-99625-enum-struct-mutually-exclusive.stderr diff --git a/tests/ui/parser/issue-99910-const-let-mutually-exclusive.fixed b/tests/ui/parser/issues/issue-99910-const-let-mutually-exclusive.fixed similarity index 100% rename from tests/ui/parser/issue-99910-const-let-mutually-exclusive.fixed rename to tests/ui/parser/issues/issue-99910-const-let-mutually-exclusive.fixed diff --git a/tests/ui/parser/issue-99910-const-let-mutually-exclusive.rs b/tests/ui/parser/issues/issue-99910-const-let-mutually-exclusive.rs similarity index 100% rename from tests/ui/parser/issue-99910-const-let-mutually-exclusive.rs rename to tests/ui/parser/issues/issue-99910-const-let-mutually-exclusive.rs diff --git a/tests/ui/parser/issue-99910-const-let-mutually-exclusive.stderr b/tests/ui/parser/issues/issue-99910-const-let-mutually-exclusive.stderr similarity index 100% rename from tests/ui/parser/issue-99910-const-let-mutually-exclusive.stderr rename to tests/ui/parser/issues/issue-99910-const-let-mutually-exclusive.stderr From 2dec1bc685c58747c81391b1c5a53ba91400c50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 24 Oct 2023 22:22:52 +0000 Subject: [PATCH 03/21] Avoid unbounded O(n^2) when parsing nested type args When encountering code like `f:: { /// appropriately. /// /// See the comments in the `parse_path_segment` function for more details. - unmatched_angle_bracket_count: u32, - max_angle_bracket_count: u32, + unmatched_angle_bracket_count: u16, + max_angle_bracket_count: u16, + angle_bracket_nesting: u16, last_unexpected_token_span: Option, /// If present, this `Parser` is not parsing Rust code but rather a macro call. @@ -394,6 +395,7 @@ impl<'a> Parser<'a> { break_last_token: false, unmatched_angle_bracket_count: 0, max_angle_bracket_count: 0, + angle_bracket_nesting: 0, last_unexpected_token_span: None, subparser_name, capture_state: CaptureState { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 2fcb9a78cfd4b..4969e672a72d5 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -487,10 +487,24 @@ impl<'a> Parser<'a> { // Take a snapshot before attempting to parse - we can restore this later. let snapshot = is_first_invocation.then(|| self.clone()); + self.angle_bracket_nesting += 1; debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)"); match self.parse_angle_args(ty_generics) { - Ok(args) => Ok(args), + Ok(args) => { + self.angle_bracket_nesting -= 1; + Ok(args) + } + Err(mut e) if self.angle_bracket_nesting > 10 => { + self.angle_bracket_nesting -= 1; + // When encountering severely malformed code where there are several levels of + // nested unclosed angle args (`f:: 0 => { + self.angle_bracket_nesting -= 1; + // Swap `self` with our backup of the parser state before attempting to parse // generic arguments. let snapshot = mem::replace(self, snapshot.unwrap()); @@ -520,8 +534,8 @@ impl<'a> Parser<'a> { // Make a span over ${unmatched angle bracket count} characters. // This is safe because `all_angle_brackets` ensures that there are only `<`s, // i.e. no multibyte characters, in this range. - let span = - lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count)); + let span = lo + .with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count.into())); self.sess.emit_err(errors::UnmatchedAngle { span, plural: snapshot.unmatched_angle_bracket_count > 1, @@ -531,7 +545,10 @@ impl<'a> Parser<'a> { self.parse_angle_args(ty_generics) } } - Err(e) => Err(e), + Err(e) => { + self.angle_bracket_nesting -= 1; + Err(e) + } } } diff --git a/tests/ui/parser/deep-unmatched-angle-brackets.rs b/tests/ui/parser/deep-unmatched-angle-brackets.rs new file mode 100644 index 0000000000000..f8d490e1c5e14 --- /dev/null +++ b/tests/ui/parser/deep-unmatched-angle-brackets.rs @@ -0,0 +1,17 @@ +trait Mul { + type Output; +} +trait Matrix: Mul<::Row, Output = ()> { + type Row; + type Transpose: Matrix; +} +fn is_mul>() {} +fn f() { + is_mul::(); + //~^ ERROR expected one of `!`, `+`, `,`, `::`, or `>`, found `(` +} +fn main() {} diff --git a/tests/ui/parser/deep-unmatched-angle-brackets.stderr b/tests/ui/parser/deep-unmatched-angle-brackets.stderr new file mode 100644 index 0000000000000..1f28503748282 --- /dev/null +++ b/tests/ui/parser/deep-unmatched-angle-brackets.stderr @@ -0,0 +1,13 @@ +error: expected one of `!`, `+`, `,`, `::`, or `>`, found `(` + --> $DIR/deep-unmatched-angle-brackets.rs:14:63 + | +LL | (); + | ^ expected one of `!`, `+`, `,`, `::`, or `>` + | +help: you might have meant to end the type parameters here + | +LL | >(); + | + + +error: aborting due to previous error + From e4c41b07f0b7fd15e19818b3349bb4bf726342d2 Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Mon, 23 Oct 2023 18:07:07 +0100 Subject: [PATCH 04/21] Add arg_count field to Body in Stable MIR This field allows SMIR consumers to identify which locals correspond to argument locals. It simply exposes the arg_count field from the MIR representation. --- compiler/rustc_smir/src/rustc_smir/mod.rs | 1 + compiler/stable_mir/src/mir/body.rs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index e772ae942fa5a..86f3c7c2e3fc6 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -308,6 +308,7 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> { span: decl.source_info.span.stable(tables), }) .collect(), + arg_count: self.arg_count, } } } diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index fc617513aeeed..34dff7a621448 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -2,10 +2,20 @@ use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability use crate::Opaque; use crate::{ty::Ty, Span}; +/// The SMIR representation of a single function. #[derive(Clone, Debug)] pub struct Body { pub blocks: Vec, + + /// Declarations of locals. + /// + /// The first local is the return value pointer, followed by `arg_count` + /// locals for the function arguments, followed by any user-declared + /// variables and temporaries. pub locals: LocalDecls, + + /// The number of arguments this function takes. + pub arg_count: usize, } type LocalDecls = Vec; From 93d1b3e92a1cd19e1609a4755a68b62e8f528707 Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Wed, 25 Oct 2023 10:21:47 +0100 Subject: [PATCH 05/21] Replace arg_count in public API with return/arg getters This commit hides the arg_count field in Body and instead exposes more stable and user-friendly methods to get the return and argument locals. As a result, Body instances must now be constructed using the `new` function. --- compiler/rustc_smir/src/rustc_smir/mod.rs | 12 +++---- compiler/stable_mir/src/mir/body.rs | 38 +++++++++++++++++++---- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 86f3c7c2e3fc6..1eb8b0ca40617 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -287,9 +287,8 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> { type T = stable_mir::mir::Body; fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { - stable_mir::mir::Body { - blocks: self - .basic_blocks + stable_mir::mir::Body::new( + self.basic_blocks .iter() .map(|block| stable_mir::mir::BasicBlock { terminator: block.terminator().stable(tables), @@ -300,16 +299,15 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> { .collect(), }) .collect(), - locals: self - .local_decls + self.local_decls .iter() .map(|decl| stable_mir::mir::LocalDecl { ty: decl.ty.stable(tables), span: decl.source_info.span.stable(tables), }) .collect(), - arg_count: self.arg_count, - } + self.arg_count, + ) } } diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 34dff7a621448..8cdc9614db672 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -8,14 +8,40 @@ pub struct Body { pub blocks: Vec, /// Declarations of locals. - /// - /// The first local is the return value pointer, followed by `arg_count` - /// locals for the function arguments, followed by any user-declared - /// variables and temporaries. + // + // The first local is the return value pointer, followed by `arg_count` + // locals for the function arguments, followed by any user-declared + // variables and temporaries. pub locals: LocalDecls, - /// The number of arguments this function takes. - pub arg_count: usize, + // The number of arguments this function takes. + arg_count: usize, +} + +impl Body { + /// Constructs a `Body`. + /// + /// A constructor is required to build a `Body` from outside the crate + /// because the `arg_count` field is private. + pub fn new(blocks: Vec, locals: LocalDecls, arg_count: usize) -> Self { + // If locals doesn't contain enough entries, it can lead to panics in + // `ret_local` and `arg_locals`. + assert!( + locals.len() >= arg_count + 1, + "A Body must contain at least a local for the return value and each of the function's arguments" + ); + Self { blocks, locals, arg_count } + } + + /// Gets the function's return local. + pub fn ret_local(&self) -> &LocalDecl { + &self.locals[0] + } + + /// Gets the locals in `self` that correspond to the function's arguments. + pub fn arg_locals(&self) -> &[LocalDecl] { + &self.locals[1..self.arg_count + 1] + } } type LocalDecls = Vec; From f4d80a5f09afcbe0bd80147a0685be1dfaf81acd Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Wed, 25 Oct 2023 16:51:18 +0100 Subject: [PATCH 06/21] Add public API to retrieve internal locals --- compiler/stable_mir/src/mir/body.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 8cdc9614db672..f76ad87584f58 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -33,15 +33,21 @@ impl Body { Self { blocks, locals, arg_count } } - /// Gets the function's return local. + /// Return local that holds this function's return value. pub fn ret_local(&self) -> &LocalDecl { &self.locals[0] } - /// Gets the locals in `self` that correspond to the function's arguments. + /// Locals in `self` that correspond to this function's arguments. pub fn arg_locals(&self) -> &[LocalDecl] { &self.locals[1..self.arg_count + 1] } + + /// Internal locals for this function. These are the locals that are + /// neither the return local nor the argument locals. + pub fn internal_locals(&self) -> &[LocalDecl] { + &self.locals[self.arg_count + 1..] + } } type LocalDecls = Vec; From 372c5335327f330858b03889a46614d2a248d325 Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Wed, 25 Oct 2023 21:28:18 +0100 Subject: [PATCH 07/21] Make locals field private --- compiler/stable_mir/src/mir/body.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index f76ad87584f58..dc472b3786c85 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -7,12 +7,12 @@ use crate::{ty::Ty, Span}; pub struct Body { pub blocks: Vec, - /// Declarations of locals. + // Declarations of locals within the function. // // The first local is the return value pointer, followed by `arg_count` // locals for the function arguments, followed by any user-declared // variables and temporaries. - pub locals: LocalDecls, + locals: LocalDecls, // The number of arguments this function takes. arg_count: usize, @@ -22,12 +22,12 @@ impl Body { /// Constructs a `Body`. /// /// A constructor is required to build a `Body` from outside the crate - /// because the `arg_count` field is private. + /// because the `arg_count` and `locals` fields are private. pub fn new(blocks: Vec, locals: LocalDecls, arg_count: usize) -> Self { // If locals doesn't contain enough entries, it can lead to panics in - // `ret_local` and `arg_locals`. + // `ret_local`, `arg_locals`, and `internal_locals`. assert!( - locals.len() >= arg_count + 1, + locals.len() > arg_count, "A Body must contain at least a local for the return value and each of the function's arguments" ); Self { blocks, locals, arg_count } From 39b293fb5a68081e7c050ed2805c8e3404c9763a Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Wed, 25 Oct 2023 22:09:12 +0100 Subject: [PATCH 08/21] Add a public API to get all body locals This is particularly helpful for the ui tests, but also could be helpful for Stable MIR users who just want all the locals without needing to concatenate responses --- compiler/stable_mir/src/mir/body.rs | 8 ++++++++ tests/ui-fulldeps/stable-mir/check_instance.rs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index dc472b3786c85..97dd649bf64ab 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -48,6 +48,14 @@ impl Body { pub fn internal_locals(&self) -> &[LocalDecl] { &self.locals[self.arg_count + 1..] } + + /// Convenience function to get all the locals in this function. + /// + /// Locals are typically accessed via the more specific methods `ret_local`, + /// `arg_locals`, and `internal_locals`. + pub fn locals(&self) -> &[LocalDecl] { + &self.locals + } } type LocalDecls = Vec; diff --git a/tests/ui-fulldeps/stable-mir/check_instance.rs b/tests/ui-fulldeps/stable-mir/check_instance.rs index ee82bc77aedae..29d88b31e77b3 100644 --- a/tests/ui-fulldeps/stable-mir/check_instance.rs +++ b/tests/ui-fulldeps/stable-mir/check_instance.rs @@ -59,7 +59,7 @@ fn test_body(body: mir::Body) { for term in body.blocks.iter().map(|bb| &bb.terminator) { match &term.kind { Call { func, .. } => { - let TyKind::RigidTy(ty) = func.ty(&body.locals).kind() else { unreachable!() }; + let TyKind::RigidTy(ty) = func.ty(&body.locals()).kind() else { unreachable!() }; let RigidTy::FnDef(def, args) = ty else { unreachable!() }; let result = Instance::resolve(def, &args); assert!(result.is_ok()); From 72e8690c0495d9ba48e27e66f824c6bdcdb1664c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 24 Oct 2023 16:19:28 +1100 Subject: [PATCH 09/21] Remove unused `never_type` feature. --- compiler/rustc_incremental/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs index bdae07a3946f5..1e8bb2c404529 100644 --- a/compiler/rustc_incremental/src/lib.rs +++ b/compiler/rustc_incremental/src/lib.rs @@ -5,7 +5,6 @@ #![cfg_attr(not(bootstrap), doc(rust_logo))] #![cfg_attr(not(bootstrap), feature(rustdoc_internals))] #![cfg_attr(not(bootstrap), allow(internal_features))] -#![feature(never_type)] #![recursion_limit = "256"] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] From ca29c272e7ab8535e1870a967a6d990c85227585 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 24 Oct 2023 16:34:03 +1100 Subject: [PATCH 10/21] Reduce exposure of three functions used only within `rustc_incremental`. --- compiler/rustc_incremental/src/lib.rs | 3 --- compiler/rustc_incremental/src/persist/fs.rs | 4 ++-- compiler/rustc_incremental/src/persist/mod.rs | 3 --- compiler/rustc_incremental/src/persist/work_product.rs | 2 +- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs index 1e8bb2c404529..e1c5e6e8d366b 100644 --- a/compiler/rustc_incremental/src/lib.rs +++ b/compiler/rustc_incremental/src/lib.rs @@ -20,13 +20,10 @@ mod persist; use assert_dep_graph::assert_dep_graph; pub use persist::copy_cgu_workproduct_to_incr_comp_cache_dir; -pub use persist::delete_workproduct_files; pub use persist::finalize_session_directory; -pub use persist::garbage_collect_session_directories; pub use persist::in_incr_comp_dir; pub use persist::in_incr_comp_dir_sess; pub use persist::load_query_result_cache; -pub use persist::prepare_session_directory; pub use persist::save_dep_graph; pub use persist::save_work_product_index; pub use persist::setup_dep_graph; diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index db8ea2bfe48aa..ecae79cbd21eb 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -202,7 +202,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu /// The garbage collection will take care of it. /// /// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph -pub fn prepare_session_directory( +pub(crate) fn prepare_session_directory( sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId, @@ -621,7 +621,7 @@ fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool { } /// Runs garbage collection for the current session. -pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> { +pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> { debug!("garbage_collect_session_directories() - begin"); let session_directory = sess.incr_comp_session_dir(); diff --git a/compiler/rustc_incremental/src/persist/mod.rs b/compiler/rustc_incremental/src/persist/mod.rs index fdecaca5a2dc1..94c05f4a2c81e 100644 --- a/compiler/rustc_incremental/src/persist/mod.rs +++ b/compiler/rustc_incremental/src/persist/mod.rs @@ -11,14 +11,11 @@ mod save; mod work_product; pub use fs::finalize_session_directory; -pub use fs::garbage_collect_session_directories; pub use fs::in_incr_comp_dir; pub use fs::in_incr_comp_dir_sess; -pub use fs::prepare_session_directory; pub use load::load_query_result_cache; pub use load::setup_dep_graph; pub use load::LoadResult; pub use save::save_dep_graph; pub use save::save_work_product_index; pub use work_product::copy_cgu_workproduct_to_incr_comp_cache_dir; -pub use work_product::delete_workproduct_files; diff --git a/compiler/rustc_incremental/src/persist/work_product.rs b/compiler/rustc_incremental/src/persist/work_product.rs index bce5ca1e16bd1..6be35a4a8dcaa 100644 --- a/compiler/rustc_incremental/src/persist/work_product.rs +++ b/compiler/rustc_incremental/src/persist/work_product.rs @@ -45,7 +45,7 @@ pub fn copy_cgu_workproduct_to_incr_comp_cache_dir( } /// Removes files for a given work product. -pub fn delete_workproduct_files(sess: &Session, work_product: &WorkProduct) { +pub(crate) fn delete_workproduct_files(sess: &Session, work_product: &WorkProduct) { for (_, path) in work_product.saved_files.items().into_sorted_stable_ord() { let path = in_incr_comp_dir_sess(sess, path); if let Err(err) = std_fs::remove_file(&path) { From 8da1b3315343bb9b77c17e6f8533fbc59af67315 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 24 Oct 2023 16:37:30 +1100 Subject: [PATCH 11/21] Move a `use` to a more sensible spot. I.e. in the source file where it's used. --- compiler/rustc_incremental/src/lib.rs | 1 - compiler/rustc_incremental/src/persist/save.rs | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs index e1c5e6e8d366b..dde138973b931 100644 --- a/compiler/rustc_incremental/src/lib.rs +++ b/compiler/rustc_incremental/src/lib.rs @@ -18,7 +18,6 @@ mod assert_dep_graph; mod errors; mod persist; -use assert_dep_graph::assert_dep_graph; pub use persist::copy_cgu_workproduct_to_incr_comp_cache_dir; pub use persist::finalize_session_directory; pub use persist::in_incr_comp_dir; diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index 210da751d95e4..fa21320be263e 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -1,3 +1,4 @@ +use crate::assert_dep_graph::assert_dep_graph; use crate::errors; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::sync::join; @@ -39,7 +40,7 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) { let dep_graph_path = dep_graph_path(sess); let staging_dep_graph_path = staging_dep_graph_path(sess); - sess.time("assert_dep_graph", || crate::assert_dep_graph(tcx)); + sess.time("assert_dep_graph", || assert_dep_graph(tcx)); sess.time("check_dirty_clean", || dirty_clean::check_dirty_clean_annotations(tcx)); if sess.opts.unstable_opts.incremental_info { From 3cf2a7441fcefa659f332b67413cc79ee5a8ac1d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Oct 2023 08:39:49 +1100 Subject: [PATCH 12/21] Tiny comment fixes. --- compiler/rustc_incremental/src/persist/fs.rs | 2 +- compiler/rustc_incremental/src/persist/load.rs | 2 +- compiler/rustc_incremental/src/persist/work_product.rs | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index ecae79cbd21eb..6d565b6d3e496 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -53,7 +53,7 @@ //! ## Synchronization //! //! There is some synchronization needed in order for the compiler to be able to -//! determine whether a given private session directory is not in used any more. +//! determine whether a given private session directory is not in use any more. //! This is done by creating a lock file for each session directory and //! locking it while the directory is still being used. Since file locks have //! operating system support, we can rely on the lock being released if the diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index 2310d0b12ef0c..cbd55fe420563 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -1,4 +1,4 @@ -//! Code to save/load the dep-graph from files. +//! Code to load the dep-graph from files. use crate::errors; use rustc_data_structures::memmap::Mmap; diff --git a/compiler/rustc_incremental/src/persist/work_product.rs b/compiler/rustc_incremental/src/persist/work_product.rs index 6be35a4a8dcaa..fb96bed5a7180 100644 --- a/compiler/rustc_incremental/src/persist/work_product.rs +++ b/compiler/rustc_incremental/src/persist/work_product.rs @@ -11,7 +11,8 @@ use rustc_session::Session; use std::fs as std_fs; use std::path::Path; -/// Copies a CGU work product to the incremental compilation directory, so next compilation can find and reuse it. +/// Copies a CGU work product to the incremental compilation directory, so next compilation can +/// find and reuse it. pub fn copy_cgu_workproduct_to_incr_comp_cache_dir( sess: &Session, cgu_name: &str, From e0c990e32c1fd061e7889703a2c0f6499e7c477d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Oct 2023 08:46:03 +1100 Subject: [PATCH 13/21] Reduce some function exposure. --- compiler/rustc_incremental/src/persist/fs.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index 6d565b6d3e496..f56fb0d0534be 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -136,26 +136,29 @@ const QUERY_CACHE_FILENAME: &str = "query-cache.bin"; const INT_ENCODE_BASE: usize = base_n::CASE_INSENSITIVE; /// Returns the path to a session's dependency graph. -pub fn dep_graph_path(sess: &Session) -> PathBuf { +pub(crate) fn dep_graph_path(sess: &Session) -> PathBuf { in_incr_comp_dir_sess(sess, DEP_GRAPH_FILENAME) } + /// Returns the path to a session's staging dependency graph. /// /// On the difference between dep-graph and staging dep-graph, /// see `build_dep_graph`. -pub fn staging_dep_graph_path(sess: &Session) -> PathBuf { +pub(crate) fn staging_dep_graph_path(sess: &Session) -> PathBuf { in_incr_comp_dir_sess(sess, STAGING_DEP_GRAPH_FILENAME) } -pub fn work_products_path(sess: &Session) -> PathBuf { + +pub(crate) fn work_products_path(sess: &Session) -> PathBuf { in_incr_comp_dir_sess(sess, WORK_PRODUCTS_FILENAME) } + /// Returns the path to a session's query cache. pub fn query_cache_path(sess: &Session) -> PathBuf { in_incr_comp_dir_sess(sess, QUERY_CACHE_FILENAME) } /// Locks a given session directory. -pub fn lock_file_path(session_dir: &Path) -> PathBuf { +fn lock_file_path(session_dir: &Path) -> PathBuf { let crate_dir = session_dir.parent().unwrap(); let directory_name = session_dir.file_name().unwrap().to_string_lossy(); @@ -373,7 +376,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Option) { let _ = garbage_collect_session_directories(sess); } -pub fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> { +pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> { let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?; for entry in sess_dir_iterator { let entry = entry?; From fe4dfb814b68bb03468736c5c0632f6495ea855e Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Thu, 26 Oct 2023 00:18:42 +0100 Subject: [PATCH 14/21] Rename internal_locals to inner_locals The word internal has connotations about information that's not exposed. It's more accurate to say that the remaining locals apply only to the inner part of the function, so I'm renaming them to inner locals. --- compiler/stable_mir/src/mir/body.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 97dd649bf64ab..75c988056b445 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -25,7 +25,7 @@ impl Body { /// because the `arg_count` and `locals` fields are private. pub fn new(blocks: Vec, locals: LocalDecls, arg_count: usize) -> Self { // If locals doesn't contain enough entries, it can lead to panics in - // `ret_local`, `arg_locals`, and `internal_locals`. + // `ret_local`, `arg_locals`, and `inner_locals`. assert!( locals.len() > arg_count, "A Body must contain at least a local for the return value and each of the function's arguments" @@ -43,16 +43,16 @@ impl Body { &self.locals[1..self.arg_count + 1] } - /// Internal locals for this function. These are the locals that are + /// Inner locals for this function. These are the locals that are /// neither the return local nor the argument locals. - pub fn internal_locals(&self) -> &[LocalDecl] { + pub fn inner_locals(&self) -> &[LocalDecl] { &self.locals[self.arg_count + 1..] } /// Convenience function to get all the locals in this function. /// /// Locals are typically accessed via the more specific methods `ret_local`, - /// `arg_locals`, and `internal_locals`. + /// `arg_locals`, and `inner_locals`. pub fn locals(&self) -> &[LocalDecl] { &self.locals } From 4b23bd47348a6325ee332e48b9d16e1b65d1c500 Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Thu, 26 Oct 2023 00:21:28 +0100 Subject: [PATCH 15/21] Update Place and Operand to take slices The latest locals() method in stable MIR returns slices instead of vecs. This commit also includes fixes to the existing tests that previously referenced the private locals field. --- compiler/stable_mir/src/mir/body.rs | 4 ++-- .../ui-fulldeps/stable-mir/check_instance.rs | 2 +- tests/ui-fulldeps/stable-mir/crate-info.rs | 22 +++++++++---------- tests/ui-fulldeps/stable-mir/smir_internal.rs | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 75c988056b445..8081def147924 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -517,7 +517,7 @@ pub enum NullOp { } impl Operand { - pub fn ty(&self, locals: &LocalDecls) -> Ty { + pub fn ty(&self, locals: &[LocalDecl]) -> Ty { match self { Operand::Copy(place) | Operand::Move(place) => place.ty(locals), Operand::Constant(c) => c.ty(), @@ -532,7 +532,7 @@ impl Constant { } impl Place { - pub fn ty(&self, locals: &LocalDecls) -> Ty { + pub fn ty(&self, locals: &[LocalDecl]) -> Ty { let _start_ty = locals[self.local].ty; todo!("Implement projection") } diff --git a/tests/ui-fulldeps/stable-mir/check_instance.rs b/tests/ui-fulldeps/stable-mir/check_instance.rs index 29d88b31e77b3..a340877752d8f 100644 --- a/tests/ui-fulldeps/stable-mir/check_instance.rs +++ b/tests/ui-fulldeps/stable-mir/check_instance.rs @@ -59,7 +59,7 @@ fn test_body(body: mir::Body) { for term in body.blocks.iter().map(|bb| &bb.terminator) { match &term.kind { Call { func, .. } => { - let TyKind::RigidTy(ty) = func.ty(&body.locals()).kind() else { unreachable!() }; + let TyKind::RigidTy(ty) = func.ty(body.locals()).kind() else { unreachable!() }; let RigidTy::FnDef(def, args) = ty else { unreachable!() }; let result = Instance::resolve(def, &args); assert!(result.is_ok()); diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 60c6053d295b4..33673cd131ff3 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -47,7 +47,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap(); let body = bar.body(); - assert_eq!(body.locals.len(), 2); + assert_eq!(body.locals().len(), 2); assert_eq!(body.blocks.len(), 1); let block = &body.blocks[0]; assert_eq!(block.statements.len(), 1); @@ -62,7 +62,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { let foo_bar = get_item(&items, (DefKind::Fn, "foo_bar")).unwrap(); let body = foo_bar.body(); - assert_eq!(body.locals.len(), 5); + assert_eq!(body.locals().len(), 5); assert_eq!(body.blocks.len(), 4); let block = &body.blocks[0]; match &block.terminator.kind { @@ -72,29 +72,29 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { let types = get_item(&items, (DefKind::Fn, "types")).unwrap(); let body = types.body(); - assert_eq!(body.locals.len(), 6); + assert_eq!(body.locals().len(), 6); assert_matches!( - body.locals[0].ty.kind(), + body.locals()[0].ty.kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) ); assert_matches!( - body.locals[1].ty.kind(), + body.locals()[1].ty.kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) ); assert_matches!( - body.locals[2].ty.kind(), + body.locals()[2].ty.kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Char) ); assert_matches!( - body.locals[3].ty.kind(), + body.locals()[3].ty.kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Int(stable_mir::ty::IntTy::I32)) ); assert_matches!( - body.locals[4].ty.kind(), + body.locals()[4].ty.kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Uint(stable_mir::ty::UintTy::U64)) ); assert_matches!( - body.locals[5].ty.kind(), + body.locals()[5].ty.kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Float( stable_mir::ty::FloatTy::F64 )) @@ -123,10 +123,10 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { for block in instance.body().blocks { match &block.terminator.kind { stable_mir::mir::TerminatorKind::Call { func, .. } => { - let TyKind::RigidTy(ty) = func.ty(&body.locals).kind() else { unreachable!() }; + let TyKind::RigidTy(ty) = func.ty(&body.locals()).kind() else { unreachable!() }; let RigidTy::FnDef(def, args) = ty else { unreachable!() }; let next_func = Instance::resolve(def, &args).unwrap(); - match next_func.body().locals[1].ty.kind() { + match next_func.body().locals()[1].ty.kind() { TyKind::RigidTy(RigidTy::Uint(_)) | TyKind::RigidTy(RigidTy::Tuple(_)) => {} other => panic!("{other:?}"), } diff --git a/tests/ui-fulldeps/stable-mir/smir_internal.rs b/tests/ui-fulldeps/stable-mir/smir_internal.rs index 5ad05559cb4bb..b0596b1882383 100644 --- a/tests/ui-fulldeps/stable-mir/smir_internal.rs +++ b/tests/ui-fulldeps/stable-mir/smir_internal.rs @@ -29,7 +29,7 @@ const CRATE_NAME: &str = "input"; fn test_translation(_tcx: TyCtxt<'_>) -> ControlFlow<()> { let main_fn = stable_mir::entry_fn().unwrap(); let body = main_fn.body(); - let orig_ty = body.locals[0].ty; + let orig_ty = body.locals()[0].ty; let rustc_ty = rustc_internal::internal(&orig_ty); assert!(rustc_ty.is_unit()); ControlFlow::Continue(()) From bac7d5b52cc60754e0555f96c3501e1531bbf6fe Mon Sep 17 00:00:00 2001 From: Kirby Linvill Date: Thu, 26 Oct 2023 00:22:56 +0100 Subject: [PATCH 16/21] Add test for smir locals --- tests/ui-fulldeps/stable-mir/crate-info.rs | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 33673cd131ff3..ed6b786f5e1de 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -140,6 +140,29 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { // Ensure we don't panic trying to get the body of a constant. foo_const.body(); + let locals_fn = get_item(&items, (DefKind::Fn, "locals")).unwrap(); + let body = locals_fn.body(); + assert_eq!(body.locals().len(), 4); + assert_matches!( + body.ret_local().ty.kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Char) + ); + assert_eq!(body.arg_locals().len(), 2); + assert_matches!( + body.arg_locals()[0].ty.kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Int(stable_mir::ty::IntTy::I32)) + ); + assert_matches!( + body.arg_locals()[1].ty.kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Uint(stable_mir::ty::UintTy::U64)) + ); + assert_eq!(body.inner_locals().len(), 1); + // If conditions have an extra inner local to hold their results + assert_matches!( + body.inner_locals()[0].ty.kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) + ); + ControlFlow::Continue(()) } @@ -211,6 +234,14 @@ fn generate_input(path: &str) -> std::io::Result<()> { pub fn assert(x: i32) -> i32 {{ x + 1 + }} + + pub fn locals(a: i32, _: u64) -> char {{ + if a > 5 {{ + 'a' + }} else {{ + 'b' + }} }}"# )?; Ok(()) From 9f5fc0283cedc2cbae3727764b451098a7e98071 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 25 Oct 2023 14:36:13 +1100 Subject: [PATCH 17/21] The value of `-Cinstrument-coverage=` doesn't need to be `Option` Not using this flag is identical to passing `-Cinstrument-coverage=off`, so there's no need to distinguish between `None` and `Some(Off)`. --- compiler/rustc_interface/src/tests.rs | 2 +- compiler/rustc_session/src/config.rs | 12 +++++------- compiler/rustc_session/src/options.rs | 18 +++++++++--------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 657bce7384b4c..d625a6b1a2c67 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -612,7 +612,7 @@ fn test_codegen_options_tracking_hash() { tracked!(force_frame_pointers, Some(false)); tracked!(force_unwind_tables, Some(true)); tracked!(inline_threshold, Some(0xf007ba11)); - tracked!(instrument_coverage, Some(InstrumentCoverage::All)); + tracked!(instrument_coverage, InstrumentCoverage::All); tracked!(link_dead_code, Some(true)); tracked!(linker_plugin_lto, LinkerPluginLto::LinkerPluginAuto); tracked!(llvm_args, vec![String::from("1"), String::from("2")]); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 78e410488c35a..7aced414ed67a 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2743,13 +2743,11 @@ pub fn build_session_options( // This is what prevents them from being used on stable compilers. match cg.instrument_coverage { // Stable values: - Some(InstrumentCoverage::All | InstrumentCoverage::Off) | None => {} + InstrumentCoverage::All | InstrumentCoverage::Off => {} // Unstable values: - Some( - InstrumentCoverage::Branch - | InstrumentCoverage::ExceptUnusedFunctions - | InstrumentCoverage::ExceptUnusedGenerics, - ) => { + InstrumentCoverage::Branch + | InstrumentCoverage::ExceptUnusedFunctions + | InstrumentCoverage::ExceptUnusedGenerics => { if !unstable_opts.unstable_options { handler.early_error( "`-C instrument-coverage=branch` and `-C instrument-coverage=except-*` \ @@ -2759,7 +2757,7 @@ pub fn build_session_options( } } - if cg.instrument_coverage.is_some() && cg.instrument_coverage != Some(InstrumentCoverage::Off) { + if cg.instrument_coverage != InstrumentCoverage::Off { if cg.profile_generate.enabled() || cg.profile_use.is_some() { handler.early_error( "option `-C instrument-coverage` is not compatible with either `-C profile-use` \ diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 35c167837e5ff..fd473acbd3c31 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -294,7 +294,7 @@ impl CodegenOptions { // JUSTIFICATION: defn of the suggested wrapper fn #[allow(rustc::bad_opt_access)] pub fn instrument_coverage(&self) -> InstrumentCoverage { - self.instrument_coverage.unwrap_or(InstrumentCoverage::Off) + self.instrument_coverage } } @@ -913,23 +913,23 @@ mod parse { } pub(crate) fn parse_instrument_coverage( - slot: &mut Option, + slot: &mut InstrumentCoverage, v: Option<&str>, ) -> bool { if v.is_some() { - let mut bool_arg = None; - if parse_opt_bool(&mut bool_arg, v) { - *slot = bool_arg.unwrap().then_some(InstrumentCoverage::All); + let mut bool_arg = false; + if parse_bool(&mut bool_arg, v) { + *slot = if bool_arg { InstrumentCoverage::All } else { InstrumentCoverage::Off }; return true; } } let Some(v) = v else { - *slot = Some(InstrumentCoverage::All); + *slot = InstrumentCoverage::All; return true; }; - *slot = Some(match v { + *slot = match v { "all" => InstrumentCoverage::All, "branch" => InstrumentCoverage::Branch, "except-unused-generics" | "except_unused_generics" => { @@ -940,7 +940,7 @@ mod parse { } "off" | "no" | "n" | "false" | "0" => InstrumentCoverage::Off, _ => return false, - }); + }; true } @@ -1352,7 +1352,7 @@ options! { inline_threshold: Option = (None, parse_opt_number, [TRACKED], "set the threshold for inlining a function"), #[rustc_lint_opt_deny_field_access("use `Session::instrument_coverage` instead of this field")] - instrument_coverage: Option = (None, parse_instrument_coverage, [TRACKED], + instrument_coverage: InstrumentCoverage = (InstrumentCoverage::Off, parse_instrument_coverage, [TRACKED], "instrument the generated code to support LLVM source-based code coverage \ reports (note, the compiler build config must include `profiler = true`); \ implies `-C symbol-mangling-version=v0`. Optional values are: From ab7f64c788d627d9fbe4119ad5e6f58eeb4da48c Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 26 Oct 2023 11:52:45 +0900 Subject: [PATCH 18/21] Revert "Remove TaKO8Ki from reviewers" This reverts commit 8e06b25e3900b8b14d9043ff6d2b846199672b2b. --- triagebot.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/triagebot.toml b/triagebot.toml index e3c4233c843b8..7cac74c2c4fac 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -625,6 +625,7 @@ compiler-team = [ compiler-team-contributors = [ "@compiler-errors", "@jackh726", + "@TaKO8Ki", "@WaffleLapkin", "@b-naber", ] @@ -671,6 +672,7 @@ diagnostics = [ "@compiler-errors", "@davidtwco", "@oli-obk", + "@TaKO8Ki", ] parser = [ "@compiler-errors", From d55487d7e9834c8fe01350b5b4fa402423c855d5 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 26 Oct 2023 12:32:47 +0200 Subject: [PATCH 19/21] Use two slice expressions to save on an offset repetition --- compiler/stable_mir/src/mir/body.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 8081def147924..d86d56b3dc724 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -40,7 +40,7 @@ impl Body { /// Locals in `self` that correspond to this function's arguments. pub fn arg_locals(&self) -> &[LocalDecl] { - &self.locals[1..self.arg_count + 1] + &self.locals[1..][..self.arg_count] } /// Inner locals for this function. These are the locals that are From d572729d59a2812229f39c217246def5f77bd420 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 26 Oct 2023 11:13:32 +0000 Subject: [PATCH 20/21] Quietly fail if an error has already occurred --- .../src/type_check/free_region_relations.rs | 5 +- .../ui/lifetimes/issue-76168-hr-outlives-3.rs | 19 +++++++ .../issue-76168-hr-outlives-3.stderr | 51 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/ui/lifetimes/issue-76168-hr-outlives-3.rs create mode 100644 tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr diff --git a/compiler/rustc_borrowck/src/type_check/free_region_relations.rs b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs index f22851d76b31a..c84256f8ff321 100644 --- a/compiler/rustc_borrowck/src/type_check/free_region_relations.rs +++ b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs @@ -8,7 +8,7 @@ use rustc_infer::infer::InferCtxt; use rustc_middle::mir::ConstraintCategory; use rustc_middle::traits::query::OutlivesBound; use rustc_middle::ty::{self, RegionVid, Ty}; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP}; use rustc_trait_selection::traits::query::type_op::{self, TypeOp}; use std::rc::Rc; use type_op::TypeOpOutput; @@ -318,7 +318,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> { .param_env .and(type_op::implied_outlives_bounds::ImpliedOutlivesBounds { ty }) .fully_perform(self.infcx, DUMMY_SP) - .unwrap_or_else(|_| bug!("failed to compute implied bounds {:?}", ty)); + .map_err(|_: ErrorGuaranteed| debug!("failed to compute implied bounds {:?}", ty)) + .ok()?; debug!(?bounds, ?constraints); self.add_outlives_bounds(bounds); constraints diff --git a/tests/ui/lifetimes/issue-76168-hr-outlives-3.rs b/tests/ui/lifetimes/issue-76168-hr-outlives-3.rs new file mode 100644 index 0000000000000..081e962028cd9 --- /dev/null +++ b/tests/ui/lifetimes/issue-76168-hr-outlives-3.rs @@ -0,0 +1,19 @@ +// edition:2018 + +#![feature(unboxed_closures)] +use std::future::Future; + +async fn wrapper(f: F) +//~^ ERROR: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` +//~| ERROR: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` +//~| ERROR: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` +where + F:, + for<'a> >::Output: Future + 'a, +{ + //~^ ERROR: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` + let mut i = 41; + &mut i; +} + +fn main() {} diff --git a/tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr b/tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr new file mode 100644 index 0000000000000..9d8c15d4a6a22 --- /dev/null +++ b/tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr @@ -0,0 +1,51 @@ +error[E0277]: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` + --> $DIR/issue-76168-hr-outlives-3.rs:6:1 + | +LL | / async fn wrapper(f: F) +LL | | +LL | | +LL | | +LL | | where +LL | | F:, +LL | | for<'a> >::Output: Future + 'a, + | |______________________________________________________________________________^ expected an `FnOnce<(&'a mut i32,)>` closure, found `i32` + | + = help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32` + +error[E0277]: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` + --> $DIR/issue-76168-hr-outlives-3.rs:6:10 + | +LL | async fn wrapper(f: F) + | ^^^^^^^ expected an `FnOnce<(&'a mut i32,)>` closure, found `i32` + | + = help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32` + +error[E0277]: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` + --> $DIR/issue-76168-hr-outlives-3.rs:13:1 + | +LL | / { +LL | | +LL | | let mut i = 41; +LL | | &mut i; +LL | | } + | |_^ expected an `FnOnce<(&'a mut i32,)>` closure, found `i32` + | + = help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32` + +error[E0277]: expected a `FnOnce<(&'a mut i32,)>` closure, found `i32` + --> $DIR/issue-76168-hr-outlives-3.rs:6:1 + | +LL | / async fn wrapper(f: F) +LL | | +LL | | +LL | | +LL | | where +LL | | F:, +LL | | for<'a> >::Output: Future + 'a, + | |______________________________________________________________________________^ expected an `FnOnce<(&'a mut i32,)>` closure, found `i32` + | + = help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0277`. From b1b1458233a7464383367e406115caa80c87cb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Thu, 26 Oct 2023 15:18:50 +0200 Subject: [PATCH 21/21] Replace type flag HAS_TY_GENERATOR with HAS_TY_COROUTINE --- compiler/rustc_middle/src/ty/flags.rs | 2 +- compiler/rustc_middle/src/ty/visit.rs | 2 +- compiler/rustc_type_ir/src/flags.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index d75315b7ac955..a348e9f608aab 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -134,7 +134,7 @@ impl FlagComputation { if should_remove_further_specializable { self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE; } - self.add_flags(TypeFlags::HAS_TY_GENERATOR); + self.add_flags(TypeFlags::HAS_TY_COROUTINE); } &ty::Closure(_, args) => { diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index 15de994864462..ab0999b3f197f 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -48,7 +48,7 @@ pub trait TypeVisitableExt<'tcx>: TypeVisitable> { self.has_type_flags(TypeFlags::HAS_TY_OPAQUE) } fn has_coroutines(&self) -> bool { - self.has_type_flags(TypeFlags::HAS_TY_GENERATOR) + self.has_type_flags(TypeFlags::HAS_TY_COROUTINE) } fn references_error(&self) -> bool { self.has_type_flags(TypeFlags::HAS_ERROR) diff --git a/compiler/rustc_type_ir/src/flags.rs b/compiler/rustc_type_ir/src/flags.rs index 2acb903c217a1..d5cadd4e83a7c 100644 --- a/compiler/rustc_type_ir/src/flags.rs +++ b/compiler/rustc_type_ir/src/flags.rs @@ -113,7 +113,7 @@ bitflags! { /// Does this value have `InferConst::Fresh`? const HAS_CT_FRESH = 1 << 22; - /// Does this have `Generator` or `GeneratorWitness`? - const HAS_TY_GENERATOR = 1 << 23; + /// Does this have `Coroutine` or `CoroutineWitness`? + const HAS_TY_COROUTINE = 1 << 23; } }