From 7366bdaea2757b7b45965f4b59a8137adf7d10af Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 29 Dec 2023 20:04:03 +0000 Subject: [PATCH 1/3] Handle ForeignItem as TAIT scope. --- .../src/collect/type_of/opaque.rs | 7 +++++++ .../nested-in-anon-const.rs | 21 +++++++++++++++++++ .../nested-in-anon-const.stderr | 20 ++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/ui/type-alias-impl-trait/nested-in-anon-const.rs create mode 100644 tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr diff --git a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs index 5a73097b0f6..04047e56c60 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs @@ -69,6 +69,7 @@ pub(super) fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: Local Node::Item(it) => locator.visit_item(it), Node::ImplItem(it) => locator.visit_impl_item(it), Node::TraitItem(it) => locator.visit_trait_item(it), + Node::ForeignItem(it) => locator.visit_foreign_item(it), other => bug!("{:?} is not a valid scope for an opaque type item", other), } } @@ -240,6 +241,12 @@ impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> { self.check(it.owner_id.def_id); intravisit::walk_trait_item(self, it); } + fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { + trace!(?it.owner_id); + assert_ne!(it.owner_id.def_id, self.def_id); + self.check(it.owner_id.def_id); + intravisit::walk_foreign_item(self, it); + } } pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>( diff --git a/tests/ui/type-alias-impl-trait/nested-in-anon-const.rs b/tests/ui/type-alias-impl-trait/nested-in-anon-const.rs new file mode 100644 index 00000000000..e9d53c99d04 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/nested-in-anon-const.rs @@ -0,0 +1,21 @@ +// Regression test for issue #119295. + +#![feature(type_alias_impl_trait)] + +type Bar = T; +type S = [i32; A]; + +extern "C" { + pub fn lint_me( + x: Bar< + S< + { //~ ERROR mismatched types + type B = impl Sized; + //~^ ERROR unconstrained opaque type + }, + >, + >, + ); +} + +fn main() {} diff --git a/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr b/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr new file mode 100644 index 00000000000..aa0c1076117 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr @@ -0,0 +1,20 @@ +error[E0308]: mismatched types + --> $DIR/nested-in-anon-const.rs:12:17 + | +LL | / { +LL | | type B = impl Sized; +LL | | +LL | | }, + | |_________________^ expected `usize`, found `()` + +error: unconstrained opaque type + --> $DIR/nested-in-anon-const.rs:13:33 + | +LL | type B = impl Sized; + | ^^^^^^^^^^ + | + = note: `B` must be used in combination with a concrete type within the same item + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. From 6dfdeab65a5e08efa811e033417bd7aef5d3c1fa Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 31 Dec 2023 00:22:52 +0000 Subject: [PATCH 2/3] Do not run check on foreign items. --- compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs index 04047e56c60..da7279967da 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs @@ -244,7 +244,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> { fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { trace!(?it.owner_id); assert_ne!(it.owner_id.def_id, self.def_id); - self.check(it.owner_id.def_id); + // No need to call `check`, as we do not run borrowck on foreign items. intravisit::walk_foreign_item(self, it); } } From eeaea576008f53e259c0f86cdd37f28bed9034a1 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 5 Jan 2024 21:54:41 +0000 Subject: [PATCH 3/3] Rebase fallout. --- .../nested-in-anon-const.stderr | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr b/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr index aa0c1076117..d0fe920b35f 100644 --- a/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr +++ b/tests/ui/type-alias-impl-trait/nested-in-anon-const.stderr @@ -1,3 +1,11 @@ +error: unconstrained opaque type + --> $DIR/nested-in-anon-const.rs:13:33 + | +LL | type B = impl Sized; + | ^^^^^^^^^^ + | + = note: `B` must be used in combination with a concrete type within the same item + error[E0308]: mismatched types --> $DIR/nested-in-anon-const.rs:12:17 | @@ -7,14 +15,6 @@ LL | | LL | | }, | |_________________^ expected `usize`, found `()` -error: unconstrained opaque type - --> $DIR/nested-in-anon-const.rs:13:33 - | -LL | type B = impl Sized; - | ^^^^^^^^^^ - | - = note: `B` must be used in combination with a concrete type within the same item - error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0308`.