Skip to content

Commit d6a18e1

Browse files
committed
change non-defining use error message
1 parent 14b0ba6 commit d6a18e1

File tree

9 files changed

+98
-47
lines changed

9 files changed

+98
-47
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use region_ctxt::RegionCtxt;
4242
/// if there are no `RegionErrors`. If there are region errors, it's likely
4343
/// that errors here are caused by them and don't need to be handled separately.
4444
pub(crate) enum DeferredOpaqueTypeError<'tcx> {
45-
/// FIXME(-Znext-solver=no): Only used by the old solver.
4645
InvalidOpaqueTypeArgs(NonDefiningUseReason<'tcx>),
4746
LifetimeMismatchOpaqueParam(LifetimeMismatchOpaqueParam<'tcx>),
4847
UnexpectedHiddenRegion {

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,6 @@ hir_analysis_supertrait_item_shadowee = item from `{$supertrait}` is shadowed by
509509
510510
hir_analysis_supertrait_item_shadowing = trait item `{$item}` from `{$subtrait}` shadows identically named item from supertrait
511511
512-
hir_analysis_tait_forward_compat2 = item does not constrain `{$opaque_type}`
513-
.note = consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`
514-
.opaque = this opaque type is supposed to be constrained
515-
516512
hir_analysis_target_feature_on_main = `main` function is not allowed to have `#[target_feature]`
517513
518514
hir_analysis_too_large_static = extern static is too large for the target architecture

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use rustc_hir::{self as hir, Expr, ImplItem, Item, Node, TraitItem, def, intravi
44
use rustc_middle::bug;
55
use rustc_middle::hir::nested_filter;
66
use rustc_middle::ty::{self, DefiningScopeKind, Ty, TyCtxt, TypeVisitableExt};
7+
use rustc_trait_selection::opaque_types::report_item_does_not_constrain_error;
78
use tracing::{debug, instrument, trace};
89

9-
use crate::errors::{TaitForwardCompat2, UnconstrainedOpaqueType};
10+
use crate::errors::UnconstrainedOpaqueType;
1011

1112
/// Checks "defining uses" of opaque `impl Trait` in associated types.
1213
/// These can only be defined by associated items of the same trait.
@@ -131,14 +132,7 @@ impl<'tcx> TaitConstraintLocator<'tcx> {
131132
// type checking the defining scope, so this error is unreachable
132133
// with the new solver.
133134
assert!(!self.tcx.next_trait_solver_globally());
134-
let guar = self.tcx.dcx().emit_err(TaitForwardCompat2 {
135-
span: self
136-
.tcx
137-
.def_ident_span(item_def_id)
138-
.unwrap_or_else(|| self.tcx.def_span(item_def_id)),
139-
opaque_type_span: self.tcx.def_span(self.def_id),
140-
opaque_type: self.tcx.def_path_str(self.def_id),
141-
});
135+
let guar = report_item_does_not_constrain_error(self.tcx, item_def_id, self.def_id, None);
142136
self.insert_found(ty::OpaqueHiddenType::new_error(self.tcx, guar));
143137
}
144138

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -410,17 +410,6 @@ pub(crate) struct UnconstrainedOpaqueType {
410410
pub what: &'static str,
411411
}
412412

413-
#[derive(Diagnostic)]
414-
#[diag(hir_analysis_tait_forward_compat2)]
415-
#[note]
416-
pub struct TaitForwardCompat2 {
417-
#[primary_span]
418-
pub span: Span,
419-
#[note(hir_analysis_opaque)]
420-
pub opaque_type_span: Span,
421-
pub opaque_type: String,
422-
}
423-
424413
pub(crate) struct MissingTypeParams {
425414
pub span: Span,
426415
pub def_span: Span,

compiler/rustc_hir_typeck/src/opaque_types.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use rustc_hir::def::DefKind;
2-
use rustc_hir_analysis::errors::TaitForwardCompat2;
32
use rustc_infer::traits::ObligationCause;
43
use rustc_middle::ty::{
54
self, DefiningScopeKind, EarlyBinder, OpaqueHiddenType, OpaqueTypeKey, TypeVisitableExt,
65
TypingMode,
76
};
87
use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
98
use rustc_trait_selection::opaque_types::{
10-
NonDefiningUseReason, opaque_type_has_defining_use_args,
9+
NonDefiningUseReason, opaque_type_has_defining_use_args, report_item_does_not_constrain_error,
1110
};
1211
use rustc_trait_selection::solve;
1312
use tracing::{debug, instrument};
@@ -43,7 +42,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
4342

4443
enum UsageKind<'tcx> {
4544
None,
46-
NonDefiningUse(OpaqueHiddenType<'tcx>),
45+
NonDefiningUse(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>),
4746
UnconstrainedHiddenType(OpaqueHiddenType<'tcx>),
4847
HasDefiningUse,
4948
}
@@ -106,18 +105,16 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
106105
if let Some(guar) = self.tainted_by_errors() {
107106
guar
108107
} else {
109-
self.tcx.dcx().emit_err(TaitForwardCompat2 {
110-
span: self
111-
.tcx
112-
.def_ident_span(self.body_id)
113-
.unwrap_or_else(|| self.tcx.def_span(self.body_id)),
114-
opaque_type_span: self.tcx.def_span(def_id),
115-
opaque_type: self.tcx.def_path_str(def_id),
116-
})
108+
report_item_does_not_constrain_error(self.tcx, self.body_id, def_id, None)
117109
}
118110
}
119-
UsageKind::NonDefiningUse(hidden_type) => {
120-
tcx.dcx().span_err(hidden_type.span, "non-defining use in the defining scope")
111+
UsageKind::NonDefiningUse(opaque_type_key, hidden_type) => {
112+
report_item_does_not_constrain_error(
113+
self.tcx,
114+
self.body_id,
115+
def_id,
116+
Some((opaque_type_key, hidden_type.span)),
117+
)
121118
}
122119
UsageKind::UnconstrainedHiddenType(hidden_type) => {
123120
let infer_var = hidden_type
@@ -166,7 +163,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
166163
);
167164
return UsageKind::HasDefiningUse;
168165
}
169-
_ => return UsageKind::NonDefiningUse(hidden_type),
166+
_ => return UsageKind::NonDefiningUse(opaque_type_key, hidden_type),
170167
};
171168
}
172169

compiler/rustc_trait_selection/src/opaque_types.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rustc_hir::def_id::LocalDefId;
44
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
55
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
66
use rustc_middle::ty::{
7-
self, DefiningScopeKind, GenericArgKind, GenericArgs, OpaqueTypeKey, TyCtxt, TypeVisitableExt,
8-
TypingMode, fold_regions,
7+
self, DefiningScopeKind, GenericArgKind, GenericArgs, OpaqueTypeKey, Ty, TyCtxt,
8+
TypeVisitableExt, TypingMode, fold_regions,
99
};
1010
use rustc_span::{ErrorGuaranteed, Span};
1111

@@ -208,3 +208,27 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
208208
canonical_args
209209
}
210210
}
211+
212+
pub fn report_item_does_not_constrain_error<'tcx>(
213+
tcx: TyCtxt<'tcx>,
214+
item_def_id: LocalDefId,
215+
def_id: LocalDefId,
216+
non_defining_use: Option<(OpaqueTypeKey<'tcx>, Span)>,
217+
) -> ErrorGuaranteed {
218+
let span = tcx.def_ident_span(item_def_id).unwrap_or_else(|| tcx.def_span(item_def_id));
219+
let opaque_type_span = tcx.def_span(def_id);
220+
let opaque_type_name = tcx.def_path_str(def_id);
221+
222+
let mut err =
223+
tcx.dcx().struct_span_err(span, format!("item does not constrain `{opaque_type_name}`"));
224+
err.note("consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`");
225+
err.span_note(opaque_type_span, "this opaque type is supposed to be constrained");
226+
if let Some((key, span)) = non_defining_use {
227+
let opaque_ty = Ty::new_opaque(tcx, key.def_id.into(), key.args);
228+
err.span_note(
229+
span,
230+
format!("this use of `{opaque_ty}` does not have unique universal generic arguments"),
231+
);
232+
}
233+
err.emit()
234+
}

tests/ui/type-alias-impl-trait/generic_nondefining_use.stderr renamed to tests/ui/type-alias-impl-trait/generic_nondefining_use.current.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0792]: expected generic type parameter, found `u32`
2-
--> $DIR/generic_nondefining_use.rs:16:21
2+
--> $DIR/generic_nondefining_use.rs:20:21
33
|
44
LL | type OneTy<T> = impl Debug;
55
| - this generic parameter must be used with a generic type parameter
@@ -8,7 +8,7 @@ LL | fn concrete_ty() -> OneTy<u32> {
88
| ^^^^^^^^^^
99

1010
error[E0792]: expected generic lifetime parameter, found `'static`
11-
--> $DIR/generic_nondefining_use.rs:23:5
11+
--> $DIR/generic_nondefining_use.rs:29:5
1212
|
1313
LL | type OneLifetime<'a> = impl Debug;
1414
| -- cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
@@ -17,7 +17,7 @@ LL | 6u32
1717
| ^^^^
1818

1919
error[E0792]: expected generic constant parameter, found `123`
20-
--> $DIR/generic_nondefining_use.rs:28:24
20+
--> $DIR/generic_nondefining_use.rs:35:24
2121
|
2222
LL | type OneConst<const X: usize> = impl Debug;
2323
| -------------- this generic parameter must be used with a generic constant parameter
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error: item does not constrain `OneTy::{opaque#0}`
2+
--> $DIR/generic_nondefining_use.rs:20:4
3+
|
4+
LL | fn concrete_ty() -> OneTy<u32> {
5+
| ^^^^^^^^^^^
6+
|
7+
= note: consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`
8+
note: this opaque type is supposed to be constrained
9+
--> $DIR/generic_nondefining_use.rs:11:17
10+
|
11+
LL | type OneTy<T> = impl Debug;
12+
| ^^^^^^^^^^
13+
note: this use of `OneTy<u32>` does not have unique universal generic arguments
14+
--> $DIR/generic_nondefining_use.rs:23:5
15+
|
16+
LL | 5u32
17+
| ^^^^
18+
19+
error: non-defining use of `OneLifetime<'_>` in the defining scope
20+
--> $DIR/generic_nondefining_use.rs:27:1
21+
|
22+
LL | fn concrete_lifetime() -> OneLifetime<'static> {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: item does not constrain `OneConst::{opaque#0}`
26+
--> $DIR/generic_nondefining_use.rs:35:4
27+
|
28+
LL | fn concrete_const() -> OneConst<{ 123 }> {
29+
| ^^^^^^^^^^^^^^
30+
|
31+
= note: consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`
32+
note: this opaque type is supposed to be constrained
33+
--> $DIR/generic_nondefining_use.rs:15:33
34+
|
35+
LL | type OneConst<const X: usize> = impl Debug;
36+
| ^^^^^^^^^^
37+
note: this use of `OneConst<123>` does not have unique universal generic arguments
38+
--> $DIR/generic_nondefining_use.rs:38:5
39+
|
40+
LL | 7u32
41+
| ^^^^
42+
43+
error: aborting due to 3 previous errors
44+

tests/ui/type-alias-impl-trait/generic_nondefining_use.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#![feature(type_alias_impl_trait)]
22

3+
//@ revisions: current next
4+
//@ ignore-compare-mode-next-solver (explicit revisions)
5+
//@[next] compile-flags: -Znext-solver
6+
37
use std::fmt::Debug;
48

59
fn main() {}
@@ -14,18 +18,22 @@ type OneConst<const X: usize> = impl Debug;
1418

1519
#[define_opaque(OneTy)]
1620
fn concrete_ty() -> OneTy<u32> {
17-
//~^ ERROR: expected generic type parameter, found `u32`
21+
//[current]~^ ERROR: expected generic type parameter, found `u32`
22+
//[next]~^^ ERROR: item does not constrain `OneTy::{opaque#0}`
1823
5u32
1924
}
2025

2126
#[define_opaque(OneLifetime)]
2227
fn concrete_lifetime() -> OneLifetime<'static> {
28+
//[next]~^ ERROR: non-defining use of `OneLifetime<'_>` in the defining scope
2329
6u32
24-
//~^ ERROR: expected generic lifetime parameter, found `'static`
30+
//[current]~^ ERROR: expected generic lifetime parameter, found `'static`
31+
2532
}
2633

2734
#[define_opaque(OneConst)]
2835
fn concrete_const() -> OneConst<{ 123 }> {
29-
//~^ ERROR: expected generic constant parameter, found `123`
36+
//[current]~^ ERROR: expected generic constant parameter, found `123`
37+
//[next]~^^ ERROR: item does not constrain `OneConst::{opaque#0}`
3038
7u32
3139
}

0 commit comments

Comments
 (0)