about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-16 13:45:30 +0200
committerGitHub <noreply@github.com>2025-04-16 13:45:30 +0200
commitec6bdda983fd0e5253cd2fc9da8ca45896a3ef9f (patch)
treea9f950ce9c550c49aa04c15c9186e42ccafe846d
parentb3284adb549ef24c4ab53e8824ec264f94665f87 (diff)
parent11e5987d017b4ab8dfc522d674aea704aad8605a (diff)
downloadrust-ec6bdda983fd0e5253cd2fc9da8ca45896a3ef9f.tar.gz
rust-ec6bdda983fd0e5253cd2fc9da8ca45896a3ef9f.zip
Rollup merge of #139880 - compiler-errors:rpitit-nameless, r=nnethercote
Don't compute name of associated item if it's an RPITIT

Use `Option::then` in favor of `Option::then_some` to not compute `AssocItem::name` if it fails the condition. Alternatively, I'd be open to changing this just to an `if`.

Fixes https://github.com/rust-lang/rust/issues/139873

r? ```@nnethercote```
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs3
-rw-r--r--tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.rs12
-rw-r--r--tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.stderr9
3 files changed, 22 insertions, 2 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
index 72f219bfeb8..3759a224ff7 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
@@ -204,8 +204,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
             .iter()
             .flat_map(|trait_def_id| tcx.associated_items(*trait_def_id).in_definition_order())
             .filter_map(|item| {
-                (!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag)
-                    .then_some(item.name())
+                (!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag).then(|| item.name())
             })
             .collect();
 
diff --git a/tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.rs b/tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.rs
new file mode 100644
index 00000000000..450f41e209d
--- /dev/null
+++ b/tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.rs
@@ -0,0 +1,12 @@
+// Regression test for <https://github.com/rust-lang/rust/issues/139873>.
+
+// Test that we don't try to get the (nonexistent) name of the RPITIT in `Trait::foo`
+// when emitting an error for a missing associated item `Trait::Output`.
+
+trait Trait {
+    fn foo() -> impl Sized;
+    fn bar() -> Self::Output;
+    //~^ ERROR associated type `Output` not found for `Self`
+}
+
+fn main() {}
diff --git a/tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.stderr b/tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.stderr
new file mode 100644
index 00000000000..74e15785af1
--- /dev/null
+++ b/tests/ui/impl-trait/in-trait/dont-probe-missing-item-name.stderr
@@ -0,0 +1,9 @@
+error[E0220]: associated type `Output` not found for `Self`
+  --> $DIR/dont-probe-missing-item-name.rs:8:23
+   |
+LL |     fn bar() -> Self::Output;
+   |                       ^^^^^^ associated type `Output` not found
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0220`.