Skip to content

Commit 990201c

Browse files
committed
move check_opaque_type_parameter_valid
1 parent abc838b commit 990201c

File tree

7 files changed

+189
-182
lines changed

7 files changed

+189
-182
lines changed

Diff for: compiler/rustc_borrowck/messages.ftl

-7
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,6 @@ borrowck_opaque_type_lifetime_mismatch =
162162
.prev_lifetime_label = lifetime `{$prev}` previously used here
163163
.note = if all non-lifetime generic parameters are the same, but the lifetime parameters differ, it is not possible to differentiate the opaque types
164164
165-
borrowck_opaque_type_non_generic_param =
166-
expected generic {$kind} parameter, found `{$ty}`
167-
.label = {STREQ($ty, "'static") ->
168-
[true] cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
169-
*[other] this generic parameter must be used with a generic {$kind} parameter
170-
}
171-
172165
borrowck_partial_var_move_by_use_in_closure =
173166
variable {$is_partial ->
174167
[true] partially moved

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+4-163
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
use rustc_data_structures::fx::FxIndexMap;
2-
use rustc_errors::ErrorGuaranteed;
3-
use rustc_hir::OpaqueTyOrigin;
4-
use rustc_hir::def_id::LocalDefId;
5-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
6-
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, TyCtxtInferExt as _};
2+
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
73
use rustc_macros::extension;
84
use rustc_middle::ty::{
9-
self, GenericArgKind, GenericArgs, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable,
10-
TypeVisitableExt, TypingMode, fold_regions,
5+
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, fold_regions,
116
};
127
use rustc_span::Span;
13-
use rustc_trait_selection::regions::OutlivesEnvironmentBuildExt;
14-
use rustc_trait_selection::traits::ObligationCtxt;
8+
use rustc_trait_selection::opaque_types::check_opaque_type_parameter_valid;
159
use tracing::{debug, instrument};
1610

1711
use super::RegionInferenceContext;
1812
use crate::opaque_types::ConcreteOpaqueTypes;
19-
use crate::session_diagnostics::{LifetimeMismatchOpaqueParam, NonGenericOpaqueTypeParam};
13+
use crate::session_diagnostics::LifetimeMismatchOpaqueParam;
2014
use crate::universal_regions::RegionClassification;
2115

2216
impl<'tcx> RegionInferenceContext<'tcx> {
@@ -289,156 +283,3 @@ impl<'tcx> InferCtxt<'tcx> {
289283
definition_ty
290284
}
291285
}
292-
293-
/// Opaque type parameter validity check as documented in the [rustc-dev-guide chapter].
294-
///
295-
/// [rustc-dev-guide chapter]:
296-
/// https://rustc-dev-guide.rust-lang.org/opaque-types-region-infer-restrictions.html
297-
fn check_opaque_type_parameter_valid<'tcx>(
298-
infcx: &InferCtxt<'tcx>,
299-
opaque_type_key: OpaqueTypeKey<'tcx>,
300-
span: Span,
301-
) -> Result<(), ErrorGuaranteed> {
302-
let tcx = infcx.tcx;
303-
let opaque_generics = tcx.generics_of(opaque_type_key.def_id);
304-
let opaque_env = LazyOpaqueTyEnv::new(tcx, opaque_type_key.def_id);
305-
let mut seen_params: FxIndexMap<_, Vec<_>> = FxIndexMap::default();
306-
307-
for (i, arg) in opaque_type_key.iter_captured_args(tcx) {
308-
let arg_is_param = match arg.unpack() {
309-
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
310-
GenericArgKind::Lifetime(lt) => {
311-
matches!(*lt, ty::ReEarlyParam(_) | ty::ReLateParam(_))
312-
|| (lt.is_static() && opaque_env.param_equal_static(i))
313-
}
314-
GenericArgKind::Const(ct) => matches!(ct.kind(), ty::ConstKind::Param(_)),
315-
};
316-
317-
if arg_is_param {
318-
// Register if the same lifetime appears multiple times in the generic args.
319-
// There is an exception when the opaque type *requires* the lifetimes to be equal.
320-
// See [rustc-dev-guide chapter] § "An exception to uniqueness rule".
321-
let seen_where = seen_params.entry(arg).or_default();
322-
if !seen_where.first().is_some_and(|&prev_i| opaque_env.params_equal(i, prev_i)) {
323-
seen_where.push(i);
324-
}
325-
} else {
326-
// Prevent `fn foo() -> Foo<u32>` from being defining.
327-
let opaque_param = opaque_generics.param_at(i, tcx);
328-
let kind = opaque_param.kind.descr();
329-
330-
opaque_env.param_is_error(i)?;
331-
332-
return Err(infcx.dcx().emit_err(NonGenericOpaqueTypeParam {
333-
ty: arg,
334-
kind,
335-
span,
336-
param_span: tcx.def_span(opaque_param.def_id),
337-
}));
338-
}
339-
}
340-
341-
for (_, indices) in seen_params {
342-
if indices.len() > 1 {
343-
let descr = opaque_generics.param_at(indices[0], tcx).kind.descr();
344-
let spans: Vec<_> = indices
345-
.into_iter()
346-
.map(|i| tcx.def_span(opaque_generics.param_at(i, tcx).def_id))
347-
.collect();
348-
#[allow(rustc::diagnostic_outside_of_impl)]
349-
#[allow(rustc::untranslatable_diagnostic)]
350-
return Err(infcx
351-
.dcx()
352-
.struct_span_err(span, "non-defining opaque type use in defining scope")
353-
.with_span_note(spans, format!("{descr} used multiple times"))
354-
.emit());
355-
}
356-
}
357-
358-
Ok(())
359-
}
360-
361-
/// Computes if an opaque type requires a lifetime parameter to be equal to
362-
/// another one or to the `'static` lifetime.
363-
/// These requirements are derived from the explicit and implied bounds.
364-
struct LazyOpaqueTyEnv<'tcx> {
365-
tcx: TyCtxt<'tcx>,
366-
def_id: LocalDefId,
367-
368-
/// Equal parameters will have the same name. Computed Lazily.
369-
/// Example:
370-
/// `type Opaque<'a: 'static, 'b: 'c, 'c: 'b> = impl Sized;`
371-
/// Identity args: `['a, 'b, 'c]`
372-
/// Canonical args: `['static, 'b, 'b]`
373-
canonical_args: std::cell::OnceCell<ty::GenericArgsRef<'tcx>>,
374-
}
375-
376-
impl<'tcx> LazyOpaqueTyEnv<'tcx> {
377-
fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
378-
Self { tcx, def_id, canonical_args: std::cell::OnceCell::new() }
379-
}
380-
381-
fn param_equal_static(&self, param_index: usize) -> bool {
382-
self.get_canonical_args()[param_index].expect_region().is_static()
383-
}
384-
385-
fn params_equal(&self, param1: usize, param2: usize) -> bool {
386-
let canonical_args = self.get_canonical_args();
387-
canonical_args[param1] == canonical_args[param2]
388-
}
389-
390-
fn param_is_error(&self, param_index: usize) -> Result<(), ErrorGuaranteed> {
391-
self.get_canonical_args()[param_index].error_reported()
392-
}
393-
394-
fn get_canonical_args(&self) -> ty::GenericArgsRef<'tcx> {
395-
if let Some(&canonical_args) = self.canonical_args.get() {
396-
return canonical_args;
397-
}
398-
399-
let &Self { tcx, def_id, .. } = self;
400-
let origin = tcx.local_opaque_ty_origin(def_id);
401-
let parent = match origin {
402-
OpaqueTyOrigin::FnReturn { parent, .. }
403-
| OpaqueTyOrigin::AsyncFn { parent, .. }
404-
| OpaqueTyOrigin::TyAlias { parent, .. } => parent,
405-
};
406-
let param_env = tcx.param_env(parent);
407-
let args = GenericArgs::identity_for_item(tcx, parent).extend_to(
408-
tcx,
409-
def_id.to_def_id(),
410-
|param, _| {
411-
tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local()).into()
412-
},
413-
);
414-
415-
// FIXME(#132279): It feels wrong to use `non_body_analysis` here given that we're
416-
// in a body here.
417-
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
418-
let ocx = ObligationCtxt::new(&infcx);
419-
420-
let wf_tys = ocx.assumed_wf_types(param_env, parent).unwrap_or_else(|_| {
421-
tcx.dcx().span_delayed_bug(tcx.def_span(def_id), "error getting implied bounds");
422-
Default::default()
423-
});
424-
let outlives_env = OutlivesEnvironment::new(&infcx, parent, param_env, wf_tys);
425-
426-
let mut seen = vec![tcx.lifetimes.re_static];
427-
let canonical_args = fold_regions(tcx, args, |r1, _| {
428-
if r1.is_error() {
429-
r1
430-
} else if let Some(&r2) = seen.iter().find(|&&r2| {
431-
let free_regions = outlives_env.free_region_map();
432-
free_regions.sub_free_regions(tcx, r1, r2)
433-
&& free_regions.sub_free_regions(tcx, r2, r1)
434-
}) {
435-
r2
436-
} else {
437-
seen.push(r1);
438-
r1
439-
}
440-
});
441-
self.canonical_args.set(canonical_args).unwrap();
442-
canonical_args
443-
}
444-
}

Diff for: compiler/rustc_borrowck/src/session_diagnostics.rs

-11
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,6 @@ pub(crate) struct MoveBorrow<'a> {
294294
pub borrow_span: Span,
295295
}
296296

297-
#[derive(Diagnostic)]
298-
#[diag(borrowck_opaque_type_non_generic_param, code = E0792)]
299-
pub(crate) struct NonGenericOpaqueTypeParam<'a, 'tcx> {
300-
pub ty: GenericArg<'tcx>,
301-
pub kind: &'a str,
302-
#[primary_span]
303-
pub span: Span,
304-
#[label]
305-
pub param_span: Span,
306-
}
307-
308297
#[derive(Diagnostic)]
309298
#[diag(borrowck_opaque_type_lifetime_mismatch)]
310299
pub(crate) struct LifetimeMismatchOpaqueParam<'tcx> {

Diff for: compiler/rustc_trait_selection/messages.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,15 @@ trait_selection_oc_no_diverge = `else` clause of `let...else` does not diverge
264264
trait_selection_oc_no_else = `if` may be missing an `else` clause
265265
trait_selection_oc_try_compat = `?` operator has incompatible types
266266
trait_selection_oc_type_compat = type not compatible with trait
267+
267268
trait_selection_opaque_captures_lifetime = hidden type for `{$opaque_ty}` captures lifetime that does not appear in bounds
268269
.label = opaque type defined here
270+
trait_selection_opaque_type_non_generic_param =
271+
expected generic {$kind} parameter, found `{$ty}`
272+
.label = {STREQ($ty, "'static") ->
273+
[true] cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
274+
*[other] this generic parameter must be used with a generic {$kind} parameter
275+
}
269276
270277
trait_selection_outlives_bound = lifetime of the source pointer does not outlive lifetime bound of the object type
271278
trait_selection_outlives_content = lifetime of reference outlives lifetime of borrowed content...

Diff for: compiler/rustc_trait_selection/src/errors.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty};
1212
use rustc_hir::{self as hir, AmbigArg, FnRetTy, GenericParamKind, IsAnonInPath, Node};
1313
use rustc_macros::{Diagnostic, Subdiagnostic};
1414
use rustc_middle::ty::print::{PrintTraitRefExt as _, TraitRefPrintOnlyTraitPath};
15-
use rustc_middle::ty::{self, Binder, ClosureKind, FnSig, Region, Ty, TyCtxt};
15+
use rustc_middle::ty::{self, Binder, ClosureKind, FnSig, GenericArg, Region, Ty, TyCtxt};
1616
use rustc_span::{BytePos, Ident, Span, Symbol, kw};
1717

1818
use crate::error_reporting::infer::ObligationCauseAsDiagArg;
@@ -1922,3 +1922,14 @@ impl Subdiagnostic for AddPreciseCapturingForOvercapture {
19221922
}
19231923
}
19241924
}
1925+
1926+
#[derive(Diagnostic)]
1927+
#[diag(trait_selection_opaque_type_non_generic_param, code = E0792)]
1928+
pub(crate) struct NonGenericOpaqueTypeParam<'a, 'tcx> {
1929+
pub ty: GenericArg<'tcx>,
1930+
pub kind: &'a str,
1931+
#[primary_span]
1932+
pub span: Span,
1933+
#[label]
1934+
pub param_span: Span,
1935+
}

Diff for: compiler/rustc_trait_selection/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
pub mod error_reporting;
3737
pub mod errors;
3838
pub mod infer;
39+
pub mod opaque_types;
3940
pub mod regions;
4041
pub mod solve;
4142
pub mod traits;

0 commit comments

Comments
 (0)