Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid opaque type not constrained errors in the presence of other errors #133850

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,18 @@ impl TaitConstraintLocator<'_> {
constrained = true;

if !opaque_types_defined_by.contains(&self.def_id) {
self.tcx.dcx().emit_err(TaitForwardCompat {
let guar = self.tcx.dcx().emit_err(TaitForwardCompat {
span: hidden_type.span,
item_span: self
.tcx
.def_ident_span(item_def_id)
.unwrap_or_else(|| self.tcx.def_span(item_def_id)),
});
// Avoid "opaque type not constrained" errors on the opaque itself.
self.found = Some(ty::OpaqueHiddenType {
span: DUMMY_SP,
ty: Ty::new_error(self.tcx, guar),
});
}
let concrete_type =
self.tcx.erase_regions(hidden_type.remap_generic_params_to_declaration_params(
Expand All @@ -248,14 +253,19 @@ impl TaitConstraintLocator<'_> {
if !constrained {
debug!("no constraints in typeck results");
if opaque_types_defined_by.contains(&self.def_id) {
self.tcx.dcx().emit_err(TaitForwardCompat2 {
let guar = self.tcx.dcx().emit_err(TaitForwardCompat2 {
span: self
.tcx
.def_ident_span(item_def_id)
.unwrap_or_else(|| self.tcx.def_span(item_def_id)),
opaque_type_span: self.tcx.def_span(self.def_id),
opaque_type: self.tcx.def_path_str(self.def_id),
});
// Avoid "opaque type not constrained" errors on the opaque itself.
self.found = Some(ty::OpaqueHiddenType {
span: DUMMY_SP,
ty: Ty::new_error(self.tcx, guar),
});
}
return;
};
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc_hir::def::CtorOf;
use rustc_index::Idx;
use tracing::trace;

use crate::rmeta::*;

Expand Down Expand Up @@ -530,8 +529,6 @@ where
{
/// Given the metadata, extract out the value at a particular index (if any).
pub(super) fn get<'a, 'tcx, M: Metadata<'a, 'tcx>>(&self, metadata: M, i: I) -> T::Value<'tcx> {
trace!("LazyTable::lookup: index={:?} len={:?}", i, self.len);

// Access past the end of the table returns a Default
if i.index() >= self.len {
return Default::default();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0284]: type annotations needed: cannot satisfy `Foo == _`
--> $DIR/norm-before-method-resolution-opaque-type.rs:16:19
--> $DIR/norm-before-method-resolution-opaque-type.rs:15:19
|
LL | fn weird_bound<X>(x: &<X as Trait<'static>>::Out<Foo>) -> X
| ^ cannot satisfy `Foo == _`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: item does not constrain `Foo::{opaque#0}`, but has it in its signature
--> $DIR/norm-before-method-resolution-opaque-type.rs:16:4
--> $DIR/norm-before-method-resolution-opaque-type.rs:15:4
|
LL | fn weird_bound<X>(x: &<X as Trait<'static>>::Out<Foo>) -> X
| ^^^^^^^^^^^
Expand All @@ -11,16 +11,8 @@ note: this opaque type is in the signature
LL | type Foo = impl Sized;
| ^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/norm-before-method-resolution-opaque-type.rs:13:12
|
LL | type Foo = impl Sized;
| ^^^^^^^^^^
|
= note: `Foo` must be used in combination with a concrete type within the same module

error[E0507]: cannot move out of `*x` which is behind a shared reference
--> $DIR/norm-before-method-resolution-opaque-type.rs:23:13
--> $DIR/norm-before-method-resolution-opaque-type.rs:22:13
|
LL | let x = *x;
| ^^ move occurs because `*x` has type `<X as Trait<'_>>::Out<Foo>`, which does not implement the `Copy` trait
Expand All @@ -31,6 +23,6 @@ LL - let x = *x;
LL + let x = x;
|

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0507`.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ impl<'a, T> Trait<'a> for T {
}

type Foo = impl Sized;
//[old]~^ ERROR: unconstrained opaque type

fn weird_bound<X>(x: &<X as Trait<'static>>::Out<Foo>) -> X
//[old]~^ ERROR: item does not constrain
Expand Down
1 change: 0 additions & 1 deletion tests/ui/impl-trait/issues/issue-86800.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct Context {
type TransactionResult<O> = Result<O, ()>;

type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
//~^ ERROR unconstrained opaque type

fn execute_transaction_fut<'f, F, O>(
//~^ ERROR: item does not constrain
Expand Down
20 changes: 6 additions & 14 deletions tests/ui/impl-trait/issues/issue-86800.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: item does not constrain `TransactionFuture::{opaque#0}`, but has it in its signature
--> $DIR/issue-86800.rs:28:4
--> $DIR/issue-86800.rs:27:4
|
LL | fn execute_transaction_fut<'f, F, O>(
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -12,7 +12,7 @@ LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResu
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: item does not constrain `TransactionFuture::{opaque#0}`, but has it in its signature
--> $DIR/issue-86800.rs:40:14
--> $DIR/issue-86800.rs:39:14
|
LL | async fn do_transaction<O>(
| ^^^^^^^^^^^^^^
Expand All @@ -25,7 +25,7 @@ LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResu
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: item does not constrain `TransactionFuture::{opaque#0}`, but has it in its signature
--> $DIR/issue-86800.rs:44:5
--> $DIR/issue-86800.rs:43:5
|
LL | / {
LL | |
Expand All @@ -43,16 +43,8 @@ note: this opaque type is in the signature
LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/issue-86800.rs:25:34
|
LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `TransactionFuture` must be used in combination with a concrete type within the same module

error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/issue-86800.rs:35:5
--> $DIR/issue-86800.rs:34:5
|
LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
| --- this generic parameter must be used with a generic lifetime parameter
Expand All @@ -61,7 +53,7 @@ LL | f
| ^

error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/issue-86800.rs:44:5
--> $DIR/issue-86800.rs:43:5
|
LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
| --- this generic parameter must be used with a generic lifetime parameter
Expand All @@ -75,6 +67,6 @@ LL | | f(&mut transaction).await
LL | | }
| |_____^

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0792`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod a {
type Foo = impl PartialEq<(Foo, i32)>;
//~^ ERROR: unconstrained opaque type

struct Bar;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0053]: method `eq` has an incompatible type for trait
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:10:30
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:9:30
|
LL | type Foo = impl PartialEq<(Foo, i32)>;
| -------------------------- the found opaque type
Expand All @@ -15,7 +15,7 @@ LL | fn eq(&self, _other: &(a::Bar, i32)) -> bool {
| ~~~~~~~~~~~~~~

error: item does not constrain `a::Foo::{opaque#0}`, but has it in its signature
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:10:12
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:9:12
|
LL | fn eq(&self, _other: &(Foo, i32)) -> bool {
| ^^
Expand All @@ -27,16 +27,8 @@ note: this opaque type is in the signature
LL | type Foo = impl PartialEq<(Foo, i32)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:4:16
|
LL | type Foo = impl PartialEq<(Foo, i32)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Foo` must be used in combination with a concrete type within the same module

error[E0053]: method `eq` has an incompatible type for trait
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:25:30
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:24:30
|
LL | type Foo = impl PartialEq<(Foo, i32)>;
| -------------------------- the expected opaque type
Expand All @@ -47,7 +39,7 @@ LL | fn eq(&self, _other: &(Bar, i32)) -> bool {
= note: expected signature `fn(&b::Bar, &(b::Foo, _)) -> _`
found signature `fn(&b::Bar, &(b::Bar, _)) -> _`
note: this item must have the opaque type in its signature in order to be able to register hidden types
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:25:12
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:24:12
|
LL | fn eq(&self, _other: &(Bar, i32)) -> bool {
| ^^
Expand All @@ -57,13 +49,13 @@ LL | fn eq(&self, _other: &(b::Foo, i32)) -> bool {
| ~~~~~~~~~~~~~~

error: unconstrained opaque type
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:19:16
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:18:16
|
LL | type Foo = impl PartialEq<(Foo, i32)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Foo` must be used in combination with a concrete type within the same module

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0053`.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ note: this opaque type is in the signature
LL | type A = impl Foo;
| ^^^^^^^^

error: unconstrained opaque type
--> $DIR/two_tait_defining_each_other2.rs:6:10
|
LL | type A = impl Foo;
| ^^^^^^^^
|
= note: `A` must be used in combination with a concrete type within the same module

error: opaque type's hidden type cannot be another opaque type from the same scope
--> $DIR/two_tait_defining_each_other2.rs:14:5
|
Expand All @@ -36,5 +28,5 @@ note: opaque type being used as hidden type
LL | type A = impl Foo;
| ^^^^^^^^

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

2 changes: 1 addition & 1 deletion tests/ui/impl-trait/two_tait_defining_each_other2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//@[next] compile-flags: -Znext-solver
#![feature(type_alias_impl_trait)]

type A = impl Foo; //[current]~ ERROR unconstrained opaque type
type A = impl Foo;
type B = impl Foo;

trait Foo {}
Expand Down
1 change: 0 additions & 1 deletion tests/ui/self/arbitrary-self-opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
struct Foo;

type Bar = impl Sized;
//~^ ERROR unconstrained opaque type

impl Foo {
fn foo(self: Bar) {}
Expand Down
14 changes: 3 additions & 11 deletions tests/ui/self/arbitrary-self-opaque.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0307]: invalid `self` parameter type: `Bar`
--> $DIR/arbitrary-self-opaque.rs:8:18
--> $DIR/arbitrary-self-opaque.rs:7:18
|
LL | fn foo(self: Bar) {}
| ^^^
Expand All @@ -8,7 +8,7 @@ LL | fn foo(self: Bar) {}
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error: item does not constrain `Bar::{opaque#0}`, but has it in its signature
--> $DIR/arbitrary-self-opaque.rs:8:8
--> $DIR/arbitrary-self-opaque.rs:7:8
|
LL | fn foo(self: Bar) {}
| ^^^
Expand All @@ -20,14 +20,6 @@ note: this opaque type is in the signature
LL | type Bar = impl Sized;
| ^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/arbitrary-self-opaque.rs:4:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
|
= note: `Bar` must be used in combination with a concrete type within the same module

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0307`.
1 change: 0 additions & 1 deletion tests/ui/type-alias-impl-trait/bad-tait-no-substs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![feature(type_alias_impl_trait)]
trait Trait<T> {}
type Alias<'a, U> = impl Trait<U>;
//~^ ERROR unconstrained opaque type

pub enum UninhabitedVariants {
Tuple(Alias),
Expand Down
22 changes: 7 additions & 15 deletions tests/ui/type-alias-impl-trait/bad-tait-no-substs.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0106]: missing lifetime specifier
--> $DIR/bad-tait-no-substs.rs:9:11
--> $DIR/bad-tait-no-substs.rs:8:11
|
LL | Tuple(Alias),
| ^^^^^ expected named lifetime parameter
Expand All @@ -11,7 +11,7 @@ LL ~ Tuple(Alias<'a>),
|

error[E0107]: missing generics for type alias `Alias`
--> $DIR/bad-tait-no-substs.rs:9:11
--> $DIR/bad-tait-no-substs.rs:8:11
|
LL | Tuple(Alias),
| ^^^^^ expected 1 generic argument
Expand All @@ -27,7 +27,7 @@ LL | Tuple(Alias<U>),
| +++

error[E0792]: non-defining opaque type use in defining scope
--> $DIR/bad-tait-no-substs.rs:9:11
--> $DIR/bad-tait-no-substs.rs:8:11
|
LL | Tuple(Alias),
| ^^^^^ argument `'_` is not a generic parameter
Expand All @@ -39,7 +39,7 @@ LL | type Alias<'a, U> = impl Trait<U>;
| ^^^^^^^^^^^^^

error: item does not constrain `Alias::{opaque#0}`, but has it in its signature
--> $DIR/bad-tait-no-substs.rs:15:4
--> $DIR/bad-tait-no-substs.rs:14:4
|
LL | fn uwu(x: UninhabitedVariants) {
| ^^^
Expand All @@ -51,22 +51,14 @@ note: this opaque type is in the signature
LL | type Alias<'a, U> = impl Trait<U>;
| ^^^^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/bad-tait-no-substs.rs:5:21
|
LL | type Alias<'a, U> = impl Trait<U>;
| ^^^^^^^^^^^^^
|
= note: `Alias` must be used in combination with a concrete type within the same module

error[E0004]: non-exhaustive patterns: `UninhabitedVariants::Tuple(_)` not covered
--> $DIR/bad-tait-no-substs.rs:17:11
--> $DIR/bad-tait-no-substs.rs:16:11
|
LL | match x {}
| ^ pattern `UninhabitedVariants::Tuple(_)` not covered
|
note: `UninhabitedVariants` defined here
--> $DIR/bad-tait-no-substs.rs:8:10
--> $DIR/bad-tait-no-substs.rs:7:10
|
LL | pub enum UninhabitedVariants {
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -80,7 +72,7 @@ LL + UninhabitedVariants::Tuple(_) => todo!(),
LL + }
|

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0004, E0106, E0107, E0792.
For more information about an error, try `rustc --explain E0004`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `Bar` is forbidden as the type of a const generic parameter
--> $DIR/const_generic_type.rs:8:24
--> $DIR/const_generic_type.rs:7:24
|
LL | async fn test<const N: crate::Bar>() {
| ^^^^^^^^^^
Expand Down
Loading
Loading