Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7204713

Browse files
authoredJan 11, 2024
Unrolled build for rust-lang#118915
Rollup merge of rust-lang#118915 - compiler-errors:alias-nits, r=lcnr Add some comments, add `can_define_opaque_ty` check to `try_normalize_ty_recur` Follow-up from rust-lang#117278, since I was recently re-reviewing this code.
2 parents 0a89233 + 427c55c commit 7204713

File tree

8 files changed

+65
-45
lines changed

8 files changed

+65
-45
lines changed
 

‎compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
315315
let mut failed = false;
316316

317317
let elaborated_args = std::iter::zip(*args, &generics.params).map(|(arg, param)| {
318-
if let Some(ty::Dynamic(obj, _, ty::DynKind::Dyn)) = arg.as_type().map(Ty::kind) {
318+
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
319319
let default = tcx.object_lifetime_default(param.def_id);
320320

321321
let re_static = tcx.lifetimes.re_static;
@@ -339,7 +339,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
339339

340340
has_dyn = true;
341341

342-
Ty::new_dynamic(tcx, obj, implied_region, ty::DynKind::Dyn).into()
342+
Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
343343
} else {
344344
arg
345345
}

‎compiler/rustc_infer/src/infer/error_reporting/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2348,11 +2348,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23482348
GenericKind::Param(ref p) => format!("the parameter type `{p}`"),
23492349
GenericKind::Placeholder(ref p) => format!("the placeholder type `{p:?}`"),
23502350
GenericKind::Alias(ref p) => match p.kind(self.tcx) {
2351-
ty::AliasKind::Projection | ty::AliasKind::Inherent => {
2351+
ty::Projection | ty::Inherent => {
23522352
format!("the associated type `{p}`")
23532353
}
2354-
ty::AliasKind::Weak => format!("the type alias `{p}`"),
2355-
ty::AliasKind::Opaque => format!("the opaque type `{p}`"),
2354+
ty::Weak => format!("the type alias `{p}`"),
2355+
ty::Opaque => format!("the opaque type `{p}`"),
23562356
},
23572357
};
23582358

‎compiler/rustc_infer/src/infer/relate/combine.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
510510
));
511511
} else {
512512
match a_ty.kind() {
513-
&ty::Alias(ty::AliasKind::Projection, data) => {
513+
&ty::Alias(ty::Projection, data) => {
514514
// FIXME: This does not handle subtyping correctly, we could
515515
// instead create a new inference variable for `a_ty`, emitting
516516
// `Projection(a_ty, a_infer)` and `a_infer <: b_ty`.
@@ -522,10 +522,9 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
522522
))
523523
}
524524
// The old solver only accepts projection predicates for associated types.
525-
ty::Alias(
526-
ty::AliasKind::Inherent | ty::AliasKind::Weak | ty::AliasKind::Opaque,
527-
_,
528-
) => return Err(TypeError::CyclicTy(a_ty)),
525+
ty::Alias(ty::Inherent | ty::Weak | ty::Opaque, _) => {
526+
return Err(TypeError::CyclicTy(a_ty));
527+
}
529528
_ => bug!("generalizated `{a_ty:?} to infer, not an alias"),
530529
}
531530
}

‎compiler/rustc_lint/src/types.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_span::{Span, Symbol};
2929
use rustc_target::abi::{Abi, Size, WrappingRange};
3030
use rustc_target::abi::{Integer, TagEncoding, Variants};
3131
use rustc_target::spec::abi::Abi as SpecAbi;
32-
use rustc_type_ir::DynKind;
3332

3433
use std::iter;
3534
use std::ops::ControlFlow;
@@ -675,7 +674,7 @@ fn lint_wide_pointer<'tcx>(
675674
}
676675
match ty.kind() {
677676
ty::RawPtr(TypeAndMut { mutbl: _, ty }) => (!ty.is_sized(cx.tcx, cx.param_env))
678-
.then(|| (refs, matches!(ty.kind(), ty::Dynamic(_, _, DynKind::Dyn)))),
677+
.then(|| (refs, matches!(ty.kind(), ty::Dynamic(_, _, ty::Dyn)))),
679678
_ => None,
680679
}
681680
};

‎compiler/rustc_middle/src/ty/sty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ impl<'tcx> AliasTy<'tcx> {
12251225

12261226
/// Whether this alias type is an opaque.
12271227
pub fn is_opaque(self, tcx: TyCtxt<'tcx>) -> bool {
1228-
matches!(self.opt_kind(tcx), Some(ty::AliasKind::Opaque))
1228+
matches!(self.opt_kind(tcx), Some(ty::Opaque))
12291229
}
12301230

12311231
/// FIXME: rename `AliasTy` to `AliasTerm` and always handle
@@ -2745,7 +2745,7 @@ impl<'tcx> Ty<'tcx> {
27452745
// Extern types have metadata = ().
27462746
| ty::Foreign(..)
27472747
// `dyn*` has no metadata
2748-
| ty::Dynamic(_, _, DynKind::DynStar)
2748+
| ty::Dynamic(_, _, ty::DynStar)
27492749
// If returned by `struct_tail_without_normalization` this is a unit struct
27502750
// without any fields, or not a struct, and therefore is Sized.
27512751
| ty::Adt(..)
@@ -2754,7 +2754,7 @@ impl<'tcx> Ty<'tcx> {
27542754
| ty::Tuple(..) => (tcx.types.unit, false),
27552755

27562756
ty::Str | ty::Slice(_) => (tcx.types.usize, false),
2757-
ty::Dynamic(_, _, DynKind::Dyn) => {
2757+
ty::Dynamic(_, _, ty::Dyn) => {
27582758
let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, None);
27592759
(tcx.type_of(dyn_metadata).instantiate(tcx, &[tail.into()]), false)
27602760
},

‎compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ use crate::rustc_smir::{alloc, Stable, Tables};
1212
impl<'tcx> Stable<'tcx> for ty::AliasKind {
1313
type T = stable_mir::ty::AliasKind;
1414
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
15-
use rustc_middle::ty::AliasKind::*;
1615
match self {
17-
Projection => stable_mir::ty::AliasKind::Projection,
18-
Inherent => stable_mir::ty::AliasKind::Inherent,
19-
Opaque => stable_mir::ty::AliasKind::Opaque,
20-
Weak => stable_mir::ty::AliasKind::Weak,
16+
ty::Projection => stable_mir::ty::AliasKind::Projection,
17+
ty::Inherent => stable_mir::ty::AliasKind::Inherent,
18+
ty::Opaque => stable_mir::ty::AliasKind::Opaque,
19+
ty::Weak => stable_mir::ty::AliasKind::Weak,
2120
}
2221
}
2322
}
@@ -34,10 +33,9 @@ impl<'tcx> Stable<'tcx> for ty::DynKind {
3433
type T = stable_mir::ty::DynKind;
3534

3635
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
37-
use rustc_middle::ty::DynKind;
3836
match self {
39-
DynKind::Dyn => stable_mir::ty::DynKind::Dyn,
40-
DynKind::DynStar => stable_mir::ty::DynKind::DynStar,
37+
ty::Dyn => stable_mir::ty::DynKind::Dyn,
38+
ty::DynStar => stable_mir::ty::DynKind::DynStar,
4139
}
4240
}
4341
}

‎compiler/rustc_trait_selection/src/solve/alias_relate.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@
22
//! Doing this via a separate goal is called "deferred alias relation" and part
33
//! of our more general approach to "lazy normalization".
44
//!
5-
//! This goal, e.g. `A alias-relate B`, may be satisfied by one of three branches:
6-
//! * normalizes-to: If `A` is a projection, we can prove the equivalent
7-
//! projection predicate with B as the right-hand side of the projection.
8-
//! This goal is computed in both directions, if both are aliases.
9-
//! * subst-relate: Equate `A` and `B` by their substs, if they're both
10-
//! aliases with the same def-id.
11-
//! * bidirectional-normalizes-to: If `A` and `B` are both projections, and both
12-
//! may apply, then we can compute the "intersection" of both normalizes-to by
13-
//! performing them together. This is used specifically to resolve ambiguities.
5+
//! This is done by first normalizing both sides of the goal, ending up in
6+
//! either a concrete type, rigid projection, opaque, or an infer variable.
7+
//! These are related further according to the rules below:
8+
//!
9+
//! (1.) If we end up with a rigid projection and a rigid projection, then we
10+
//! relate those projections structurally.
11+
//!
12+
//! (2.) If we end up with a rigid projection and an alias, then the opaque will
13+
//! have its hidden type defined to be that rigid projection.
14+
//!
15+
//! (3.) If we end up with an opaque and an opaque, then we assemble two
16+
//! candidates, one defining the LHS to be the hidden type of the RHS, and vice
17+
//! versa.
18+
//!
19+
//! (4.) If we end up with an infer var and an opaque or rigid projection, then
20+
//! we assign the alias to the infer var.
21+
//!
22+
//! (5.) If we end up with an opaque and a rigid (non-projection) type, then we
23+
//! define the hidden type of the opaque to be the rigid type.
24+
//!
25+
//! (6.) Otherwise, if we end with two rigid (non-projection) or infer types,
26+
//! relate them structurally.
27+
1428
use super::{EvalCtxt, GoalSource};
1529
use rustc_infer::infer::DefineOpaqueTypes;
1630
use rustc_infer::traits::query::NoSolution;
@@ -50,6 +64,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5064
self.relate(param_env, lhs, variance, rhs)?;
5165
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
5266
} else if alias.is_opaque(tcx) {
67+
// FIXME: This doesn't account for variance.
5368
self.define_opaque(param_env, alias, rhs)
5469
} else {
5570
Err(NoSolution)
@@ -60,6 +75,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
6075
self.relate(param_env, lhs, variance, rhs)?;
6176
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
6277
} else if alias.is_opaque(tcx) {
78+
// FIXME: This doesn't account for variance.
6379
self.define_opaque(param_env, alias, lhs)
6480
} else {
6581
Err(NoSolution)
@@ -72,6 +88,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
7288
}
7389
}
7490

91+
// FIXME: This needs a name that reflects that it's okay to bottom-out with an inference var.
7592
/// Normalize the `term` to equate it later. This does not define opaque types.
7693
#[instrument(level = "debug", skip(self, param_env), ret)]
7794
fn try_normalize_term(

‎compiler/rustc_trait_selection/src/solve/mod.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_middle::traits::solve::{
2222
CanonicalResponse, Certainty, ExternalConstraintsData, Goal, GoalSource, IsNormalizesToHack,
2323
QueryResult, Response,
2424
};
25+
use rustc_middle::traits::Reveal;
2526
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, UniverseIndex};
2627
use rustc_middle::ty::{
2728
CoercePredicate, RegionOutlivesPredicate, SubtypePredicate, TypeOutlivesPredicate,
@@ -316,19 +317,25 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
316317
return Some(ty);
317318
};
318319

319-
// We do no always define opaque types eagerly to allow non-defining uses in the defining scope.
320-
if let (DefineOpaqueTypes::No, ty::AliasKind::Opaque) = (define_opaque_types, kind) {
321-
if let Some(def_id) = alias.def_id.as_local() {
322-
if self
323-
.unify_existing_opaque_tys(
324-
param_env,
325-
OpaqueTypeKey { def_id, args: alias.args },
326-
self.next_ty_infer(),
327-
)
328-
.is_empty()
329-
{
330-
return Some(ty);
331-
}
320+
// We do no always define opaque types eagerly to allow non-defining uses
321+
// in the defining scope. However, if we can unify this opaque to an existing
322+
// opaque, then we should attempt to eagerly reveal the opaque, and we fall
323+
// through.
324+
if let DefineOpaqueTypes::No = define_opaque_types
325+
&& let Reveal::UserFacing = param_env.reveal()
326+
&& let ty::Opaque = kind
327+
&& let Some(def_id) = alias.def_id.as_local()
328+
&& self.can_define_opaque_ty(def_id)
329+
{
330+
if self
331+
.unify_existing_opaque_tys(
332+
param_env,
333+
OpaqueTypeKey { def_id, args: alias.args },
334+
self.next_ty_infer(),
335+
)
336+
.is_empty()
337+
{
338+
return Some(ty);
332339
}
333340
}
334341

0 commit comments

Comments
 (0)
Please sign in to comment.