Skip to content

Commit 444a627

Browse files
committed
Auto merge of rust-lang#141292 - matthiaskrgr:rollup-9nhhk7k, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#139419 (Error on recursive opaque ty in HIR typeck) - rust-lang#141236 (Resolved issue with mismatched types triggering ICE in certain scenarios) - rust-lang#141253 (Warning added when dependency crate has async drop types, and the feature is disabled) - rust-lang#141269 (rustc-dev-guide subtree update) - rust-lang#141275 (`gather_locals`: only visit guard pattern guards when checking the guard) - rust-lang#141279 (`lower_to_hir` cleanups) - rust-lang#141285 (Add tick to `RePlaceholder` debug output) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6cab15c + cc0ee34 commit 444a627

40 files changed

+279
-303
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,32 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
7070
}
7171
}
7272

73-
pub(super) fn lower_node(&mut self, def_id: LocalDefId) -> hir::MaybeOwner<'hir> {
73+
pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
7474
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
7575
if let hir::MaybeOwner::Phantom = owner {
7676
let node = self.ast_index[def_id];
7777
match node {
7878
AstOwner::NonOwner => {}
79-
AstOwner::Crate(c) => self.lower_crate(c),
80-
AstOwner::Item(item) => self.lower_item(item),
81-
AstOwner::AssocItem(item, ctxt) => self.lower_assoc_item(item, ctxt),
82-
AstOwner::ForeignItem(item) => self.lower_foreign_item(item),
79+
AstOwner::Crate(c) => {
80+
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
81+
self.with_lctx(CRATE_NODE_ID, |lctx| {
82+
let module = lctx.lower_mod(&c.items, &c.spans);
83+
// FIXME(jdonszelman): is dummy span ever a problem here?
84+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
85+
hir::OwnerNode::Crate(module)
86+
})
87+
}
88+
AstOwner::Item(item) => {
89+
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
90+
}
91+
AstOwner::AssocItem(item, ctxt) => {
92+
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
93+
}
94+
AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
95+
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
96+
}),
8397
}
8498
}
85-
86-
self.owners[def_id]
87-
}
88-
89-
#[instrument(level = "debug", skip(self, c))]
90-
fn lower_crate(&mut self, c: &Crate) {
91-
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
92-
self.with_lctx(CRATE_NODE_ID, |lctx| {
93-
let module = lctx.lower_mod(&c.items, &c.spans);
94-
// FIXME(jdonszelman): is dummy span ever a problem here?
95-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
96-
hir::OwnerNode::Crate(module)
97-
})
98-
}
99-
100-
#[instrument(level = "debug", skip(self))]
101-
fn lower_item(&mut self, item: &Item) {
102-
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
103-
}
104-
105-
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) {
106-
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
107-
}
108-
109-
fn lower_foreign_item(&mut self, item: &ForeignItem) {
110-
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
11199
}
112100
}
113101

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,14 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
444444
tcx.definitions_untracked().def_index_count(),
445445
);
446446

447+
let mut lowerer = item::ItemLowerer {
448+
tcx,
449+
resolver: &mut resolver,
450+
ast_index: &ast_index,
451+
owners: &mut owners,
452+
};
447453
for def_id in ast_index.indices() {
448-
item::ItemLowerer {
449-
tcx,
450-
resolver: &mut resolver,
451-
ast_index: &ast_index,
452-
owners: &mut owners,
453-
}
454-
.lower_node(def_id);
454+
lowerer.lower_node(def_id);
455455
}
456456

457457
drop(ast_index);

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8484

8585
self.annotate_expected_due_to_let_ty(err, expr, error);
8686
self.annotate_loop_expected_due_to_inference(err, expr, error);
87-
if self.annotate_mut_binding_to_immutable_binding(err, expr, error) {
87+
if self.annotate_mut_binding_to_immutable_binding(err, expr, expr_ty, expected, error) {
8888
return;
8989
}
9090

@@ -799,17 +799,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799799
/// Detect the following case
800800
///
801801
/// ```text
802-
/// fn change_object(mut a: &Ty) {
802+
/// fn change_object(mut b: &Ty) {
803803
/// let a = Ty::new();
804804
/// b = a;
805805
/// }
806806
/// ```
807807
///
808-
/// where the user likely meant to modify the value behind there reference, use `a` as an out
808+
/// where the user likely meant to modify the value behind there reference, use `b` as an out
809809
/// parameter, instead of mutating the local binding. When encountering this we suggest:
810810
///
811811
/// ```text
812-
/// fn change_object(a: &'_ mut Ty) {
812+
/// fn change_object(b: &'_ mut Ty) {
813813
/// let a = Ty::new();
814814
/// *b = a;
815815
/// }
@@ -818,13 +818,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
818818
&self,
819819
err: &mut Diag<'_>,
820820
expr: &hir::Expr<'_>,
821+
expr_ty: Ty<'tcx>,
822+
expected: Ty<'tcx>,
821823
error: Option<TypeError<'tcx>>,
822824
) -> bool {
823-
if let Some(TypeError::Sorts(ExpectedFound { expected, found })) = error
825+
if let Some(TypeError::Sorts(ExpectedFound { .. })) = error
824826
&& let ty::Ref(_, inner, hir::Mutability::Not) = expected.kind()
825827

826828
// The difference between the expected and found values is one level of borrowing.
827-
&& self.can_eq(self.param_env, *inner, found)
829+
&& self.can_eq(self.param_env, *inner, expr_ty)
828830

829831
// We have an `ident = expr;` assignment.
830832
&& let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(lhs, rhs, _), .. }) =

compiler/rustc_hir_typeck/src/gather_locals.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,12 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
218218
);
219219
}
220220
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.take();
221-
intravisit::walk_pat(self, p);
221+
if let PatKind::Guard(subpat, _) = p.kind {
222+
// We'll visit the guard when checking it. Don't gather its locals twice.
223+
self.visit_pat(subpat);
224+
} else {
225+
intravisit::walk_pat(self, p);
226+
}
222227
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
223228
}
224229

compiler/rustc_hir_typeck/src/writeback.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
//! which creates a new `TypeckResults` which doesn't contain any inference variables.
1010
1111
use std::mem;
12+
use std::ops::ControlFlow;
1213

14+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1315
use rustc_data_structures::unord::ExtendUnord;
14-
use rustc_errors::ErrorGuaranteed;
16+
use rustc_errors::{E0720, ErrorGuaranteed};
17+
use rustc_hir::def_id::LocalDefId;
1518
use rustc_hir::intravisit::{self, InferKind, Visitor};
1619
use rustc_hir::{self as hir, AmbigArg, HirId};
1720
use rustc_infer::traits::solve::Goal;
1821
use rustc_middle::traits::ObligationCause;
1922
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
2023
use rustc_middle::ty::{
21-
self, DefiningScopeKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
22-
TypeVisitableExt, fold_regions,
24+
self, DefiningScopeKind, OpaqueHiddenType, Ty, TyCtxt, TypeFoldable, TypeFolder,
25+
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
26+
fold_regions,
2327
};
2428
use rustc_span::{Span, sym};
2529
use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
@@ -595,6 +599,35 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
595599
entry.span = prev.span.substitute_dummy(hidden_type.span);
596600
}
597601
}
602+
603+
let recursive_opaques: Vec<_> = self
604+
.typeck_results
605+
.concrete_opaque_types
606+
.iter()
607+
.filter(|&(&def_id, hidden_ty)| {
608+
hidden_ty
609+
.ty
610+
.visit_with(&mut HasRecursiveOpaque {
611+
def_id,
612+
seen: Default::default(),
613+
opaques: &self.typeck_results.concrete_opaque_types,
614+
tcx,
615+
})
616+
.is_break()
617+
})
618+
.map(|(def_id, hidden_ty)| (*def_id, hidden_ty.span))
619+
.collect();
620+
for (def_id, span) in recursive_opaques {
621+
let guar = self
622+
.fcx
623+
.dcx()
624+
.struct_span_err(span, "cannot resolve opaque type")
625+
.with_code(E0720)
626+
.emit();
627+
self.typeck_results
628+
.concrete_opaque_types
629+
.insert(def_id, OpaqueHiddenType { span, ty: Ty::new_error(tcx, guar) });
630+
}
598631
}
599632

600633
fn visit_field_id(&mut self, hir_id: HirId) {
@@ -959,3 +992,34 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EagerlyNormalizeConsts<'tcx> {
959992
self.tcx.try_normalize_erasing_regions(self.typing_env, ct).unwrap_or(ct)
960993
}
961994
}
995+
996+
struct HasRecursiveOpaque<'a, 'tcx> {
997+
def_id: LocalDefId,
998+
seen: FxHashSet<LocalDefId>,
999+
opaques: &'a FxIndexMap<LocalDefId, ty::OpaqueHiddenType<'tcx>>,
1000+
tcx: TyCtxt<'tcx>,
1001+
}
1002+
1003+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for HasRecursiveOpaque<'_, 'tcx> {
1004+
type Result = ControlFlow<()>;
1005+
1006+
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
1007+
if let ty::Alias(ty::Opaque, alias_ty) = *t.kind()
1008+
&& let Some(def_id) = alias_ty.def_id.as_local()
1009+
{
1010+
if self.def_id == def_id {
1011+
return ControlFlow::Break(());
1012+
}
1013+
1014+
if self.seen.insert(def_id)
1015+
&& let Some(hidden_ty) = self.opaques.get(&def_id)
1016+
{
1017+
ty::EarlyBinder::bind(hidden_ty.ty)
1018+
.instantiate(self.tcx, alias_ty.args)
1019+
.visit_with(self)?;
1020+
}
1021+
}
1022+
1023+
t.super_visit_with(self)
1024+
}
1025+
}

compiler/rustc_metadata/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ metadata_as_needed_compatibility =
22
linking modifier `as-needed` is only compatible with `dylib` and `framework` linking kinds
33
44
metadata_async_drop_types_in_dependency =
5-
found async drop types in dependecy `{$extern_crate}`, but async_drop feature is disabled for `{$local_crate}`
5+
found async drop types in dependency `{$extern_crate}`, but async_drop feature is disabled for `{$local_crate}`
66
.help = if async drop type will be dropped in a crate without `feature(async_drop)`, sync Drop will be used
77
88
metadata_bad_panic_strategy =

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::fmt;
22
use std::iter::once;
3-
use std::ops::ControlFlow;
43

54
use rustc_abi::{FIRST_VARIANT, FieldIdx, Integer, VariantIdx};
65
use rustc_arena::DroplessArena;
@@ -12,8 +11,7 @@ use rustc_middle::mir::{self, Const};
1211
use rustc_middle::thir::{self, Pat, PatKind, PatRange, PatRangeBoundary};
1312
use rustc_middle::ty::layout::IntegerExt;
1413
use rustc_middle::ty::{
15-
self, FieldDef, OpaqueTypeKey, ScalarInt, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
16-
TypeVisitableExt, TypeVisitor, VariantDef,
14+
self, FieldDef, OpaqueTypeKey, ScalarInt, Ty, TyCtxt, TypeVisitableExt, VariantDef,
1715
};
1816
use rustc_middle::{bug, span_bug};
1917
use rustc_session::lint;
@@ -137,22 +135,11 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
137135
/// Returns the hidden type corresponding to this key if the body under analysis is allowed to
138136
/// know it.
139137
fn reveal_opaque_key(&self, key: OpaqueTypeKey<'tcx>) -> Option<Ty<'tcx>> {
140-
if let Some(hidden_ty) = self.typeck_results.concrete_opaque_types.get(&key.def_id) {
141-
let ty = ty::EarlyBinder::bind(hidden_ty.ty).instantiate(self.tcx, key.args);
142-
if ty.visit_with(&mut RecursiveOpaque { def_id: key.def_id.into() }).is_continue() {
143-
Some(ty)
144-
} else {
145-
// HACK: We skip revealing opaque types which recursively expand
146-
// to themselves. This is because we may infer hidden types like
147-
// `Opaque<T> = Opaque<Opaque<T>>` or `Opaque<T> = Opaque<(T,)>`
148-
// in hir typeck.
149-
None
150-
}
151-
} else {
152-
None
153-
}
138+
self.typeck_results
139+
.concrete_opaque_types
140+
.get(&key.def_id)
141+
.map(|x| ty::EarlyBinder::bind(x.ty).instantiate(self.tcx, key.args))
154142
}
155-
156143
// This can take a non-revealed `Ty` because it reveals opaques itself.
157144
pub fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
158145
!ty.inhabited_predicate(self.tcx).apply_revealing_opaque(
@@ -1177,20 +1164,3 @@ fn detect_mixed_deref_pat_ctors<'p, 'tcx>(
11771164
}
11781165
Ok(())
11791166
}
1180-
1181-
struct RecursiveOpaque {
1182-
def_id: DefId,
1183-
}
1184-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for RecursiveOpaque {
1185-
type Result = ControlFlow<()>;
1186-
1187-
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
1188-
if let ty::Alias(ty::Opaque, alias_ty) = t.kind() {
1189-
if alias_ty.def_id == self.def_id {
1190-
return ControlFlow::Break(());
1191-
}
1192-
}
1193-
1194-
if t.has_opaque_types() { t.super_visit_with(self) } else { ControlFlow::Continue(()) }
1195-
}
1196-
}

compiler/rustc_type_ir/src/region_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<I: Interner> fmt::Debug for RegionKind<I> {
193193

194194
ReVar(vid) => write!(f, "{vid:?}"),
195195

196-
RePlaceholder(placeholder) => write!(f, "{placeholder:?}"),
196+
RePlaceholder(placeholder) => write!(f, "'{placeholder:?}"),
197197

198198
// Use `'{erased}` as the output instead of `'erased` so that its more obviously distinct from
199199
// a `ReEarlyParam` named `'erased`. Technically that would print as `'erased/#IDX` so this is

src/doc/rustc-dev-guide/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
414482f6a0d4e7290f614300581a0b55442552a3
1+
e42bbfe1f7c26f8760a99c4b1f27d33aba1040bb

src/doc/rustc-dev-guide/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@
134134

135135
- [Command-line arguments](./cli.md)
136136
- [rustc_driver and rustc_interface](./rustc-driver/intro.md)
137+
- [Remarks on perma-unstable features](./rustc-driver/remarks-on-perma-unstable-features.md)
137138
- [Example: Type checking](./rustc-driver/interacting-with-the-ast.md)
138139
- [Example: Getting diagnostics](./rustc-driver/getting-diagnostics.md)
139-
- [Remarks on perma-unstable features](./rustc-driver/remarks-on-perma-unstable-features.md)
140140
- [Errors and lints](diagnostics.md)
141141
- [Diagnostic and subdiagnostic structs](./diagnostics/diagnostic-structs.md)
142142
- [Translation](./diagnostics/translation.md)

0 commit comments

Comments
 (0)