Skip to content

Commit 8a3ad49

Browse files
authored
Rollup merge of #97351 - b-naber:adt-const-params-structural-match-violation, r=michaelwoerister
Output correct type responsible for structural match violation Previously we included the outermost type that caused a structural match violation in the error message and stated that that type must be annotated with `#[derive(Eq, PartialEq)]` even if it already had that annotation. This PR outputs the correct type in the error message. Fixes #97278
2 parents 0067078 + 86e8bbe commit 8a3ad49

File tree

7 files changed

+73
-30
lines changed

7 files changed

+73
-30
lines changed

Diff for: compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,27 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
121121

122122
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
123123
traits::search_for_structural_match_violation(self.span, self.tcx(), ty).map(|non_sm_ty| {
124-
with_no_trimmed_paths!(match non_sm_ty {
125-
traits::NonStructuralMatchTy::Adt(adt) => self.adt_derive_msg(adt),
126-
traits::NonStructuralMatchTy::Dynamic => {
124+
with_no_trimmed_paths!(match non_sm_ty.kind {
125+
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
126+
traits::NonStructuralMatchTyKind::Dynamic => {
127127
"trait objects cannot be used in patterns".to_string()
128128
}
129-
traits::NonStructuralMatchTy::Opaque => {
129+
traits::NonStructuralMatchTyKind::Opaque => {
130130
"opaque types cannot be used in patterns".to_string()
131131
}
132-
traits::NonStructuralMatchTy::Closure => {
132+
traits::NonStructuralMatchTyKind::Closure => {
133133
"closures cannot be used in patterns".to_string()
134134
}
135-
traits::NonStructuralMatchTy::Generator => {
135+
traits::NonStructuralMatchTyKind::Generator => {
136136
"generators cannot be used in patterns".to_string()
137137
}
138-
traits::NonStructuralMatchTy::Param => {
138+
traits::NonStructuralMatchTyKind::Param => {
139139
bug!("use of a constant whose type is a parameter inside a pattern")
140140
}
141-
traits::NonStructuralMatchTy::Projection => {
141+
traits::NonStructuralMatchTyKind::Projection => {
142142
bug!("use of a constant whose type is a projection inside a pattern")
143143
}
144-
traits::NonStructuralMatchTy::Foreign => {
144+
traits::NonStructuralMatchTyKind::Foreign => {
145145
bug!("use of a value of a foreign type inside a pattern")
146146
}
147147
})

Diff for: compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use self::specialize::specialization_graph::FutureCompatOverlapError;
6262
pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
6363
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
6464
pub use self::structural_match::search_for_structural_match_violation;
65-
pub use self::structural_match::NonStructuralMatchTy;
65+
pub use self::structural_match::{NonStructuralMatchTy, NonStructuralMatchTyKind};
6666
pub use self::util::{
6767
elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
6868
elaborate_trait_ref, elaborate_trait_refs,

Diff for: compiler/rustc_trait_selection/src/traits/structural_match.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ use rustc_span::Span;
1111
use std::ops::ControlFlow;
1212

1313
#[derive(Debug)]
14-
pub enum NonStructuralMatchTy<'tcx> {
14+
pub struct NonStructuralMatchTy<'tcx> {
15+
pub ty: Ty<'tcx>,
16+
pub kind: NonStructuralMatchTyKind<'tcx>,
17+
}
18+
19+
#[derive(Debug)]
20+
pub enum NonStructuralMatchTyKind<'tcx> {
1521
Adt(AdtDef<'tcx>),
1622
Param,
1723
Dynamic,
@@ -137,25 +143,32 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
137143
let (adt_def, substs) = match *ty.kind() {
138144
ty::Adt(adt_def, substs) => (adt_def, substs),
139145
ty::Param(_) => {
140-
return ControlFlow::Break(NonStructuralMatchTy::Param);
146+
let kind = NonStructuralMatchTyKind::Param;
147+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
141148
}
142149
ty::Dynamic(..) => {
143-
return ControlFlow::Break(NonStructuralMatchTy::Dynamic);
150+
let kind = NonStructuralMatchTyKind::Dynamic;
151+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
144152
}
145153
ty::Foreign(_) => {
146-
return ControlFlow::Break(NonStructuralMatchTy::Foreign);
154+
let kind = NonStructuralMatchTyKind::Foreign;
155+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
147156
}
148157
ty::Opaque(..) => {
149-
return ControlFlow::Break(NonStructuralMatchTy::Opaque);
158+
let kind = NonStructuralMatchTyKind::Opaque;
159+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
150160
}
151161
ty::Projection(..) => {
152-
return ControlFlow::Break(NonStructuralMatchTy::Projection);
162+
let kind = NonStructuralMatchTyKind::Projection;
163+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
153164
}
154165
ty::Closure(..) => {
155-
return ControlFlow::Break(NonStructuralMatchTy::Closure);
166+
let kind = NonStructuralMatchTyKind::Closure;
167+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
156168
}
157169
ty::Generator(..) | ty::GeneratorWitness(..) => {
158-
return ControlFlow::Break(NonStructuralMatchTy::Generator);
170+
let kind = NonStructuralMatchTyKind::Generator;
171+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
159172
}
160173
ty::RawPtr(..) => {
161174
// structural-match ignores substructure of
@@ -215,7 +228,8 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
215228

216229
if !self.type_marked_structural(ty) {
217230
debug!("Search found ty: {:?}", ty);
218-
return ControlFlow::Break(NonStructuralMatchTy::Adt(adt_def));
231+
let kind = NonStructuralMatchTyKind::Adt(adt_def);
232+
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
219233
}
220234

221235
// structural-match does not care about the

Diff for: compiler/rustc_typeck/src/check/wfcheck.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,9 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
827827
);
828828
}
829829

830-
if traits::search_for_structural_match_violation(param.span, tcx, ty).is_some() {
830+
if let Some(non_structural_match_ty) =
831+
traits::search_for_structural_match_violation(param.span, tcx, ty)
832+
{
831833
// We use the same error code in both branches, because this is really the same
832834
// issue: we just special-case the message for type parameters to make it
833835
// clearer.
@@ -853,19 +855,23 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
853855
)
854856
.emit();
855857
} else {
856-
struct_span_err!(
858+
let mut diag = struct_span_err!(
857859
tcx.sess,
858860
hir_ty.span,
859861
E0741,
860862
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
861863
the type of a const parameter",
862-
ty,
863-
)
864-
.span_label(
865-
hir_ty.span,
866-
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
867-
)
868-
.emit();
864+
non_structural_match_ty.ty,
865+
);
866+
867+
if ty == non_structural_match_ty.ty {
868+
diag.span_label(
869+
hir_ty.span,
870+
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
871+
);
872+
}
873+
874+
diag.emit();
869875
}
870876
}
871877
} else {

Diff for: src/test/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0741]: `&'static (dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
1+
error[E0741]: `(dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
22
--> $DIR/issue-63322-forbid-dyn.rs:9:18
33
|
44
LL | fn test<const T: &'static dyn A>() {
5-
| ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq`
5+
| ^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

Diff for: src/test/ui/const-generics/issues/issue-97278.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(adt_const_params)]
2+
#![allow(incomplete_features)]
3+
4+
use std::sync::Arc;
5+
6+
#[derive(PartialEq, Eq)]
7+
enum Bar {
8+
Bar(Arc<i32>)
9+
}
10+
11+
fn test<const BAR: Bar>() {}
12+
//~^ ERROR `Arc<i32>` must be annotated with `#[derive(PartialEq, Eq)]`
13+
14+
fn main() {}

Diff for: src/test/ui/const-generics/issues/issue-97278.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0741]: `Arc<i32>` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
2+
--> $DIR/issue-97278.rs:11:20
3+
|
4+
LL | fn test<const BAR: Bar>() {}
5+
| ^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0741`.

0 commit comments

Comments
 (0)