Skip to content

Commit 9ff82a8

Browse files
authored
Rollup merge of rust-lang#120270 - compiler-errors:randos, r=lcnr
A bunch of random modifications r? oli-obk Kitchen sink of changes that I didn't know where to put elsewhere. Documentation tweaks mostly, but also removing some unreachable code and simplifying the pretty printing for closures/coroutines.
2 parents 9db841e + 5fc39e0 commit 9ff82a8

File tree

13 files changed

+59
-93
lines changed

13 files changed

+59
-93
lines changed

compiler/rustc_hir_analysis/src/autoderef.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use rustc_trait_selection::traits::StructurallyNormalizeExt;
1212

1313
#[derive(Copy, Clone, Debug)]
1414
pub enum AutoderefKind {
15+
/// A true pointer type, such as `&T` and `*mut T`.
1516
Builtin,
17+
/// A type which must dispatch to a `Deref` implementation.
1618
Overloaded,
1719
}
1820

@@ -83,6 +85,7 @@ impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
8385
(AutoderefKind::Builtin, ty)
8486
}
8587
} else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
88+
// The overloaded deref check already normalizes the pointee type.
8689
(AutoderefKind::Overloaded, ty)
8790
} else {
8891
return None;

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn compare_method_predicate_entailment<'tcx>(
254254
// checks. For the comparison to be valid, we need to
255255
// normalize the associated types in the impl/trait methods
256256
// first. However, because function types bind regions, just
257-
// calling `normalize_associated_types_in` would have no effect on
257+
// calling `FnCtxt::normalize` would have no effect on
258258
// any associated types appearing in the fn arguments or return
259259
// type.
260260

compiler/rustc_hir_typeck/src/method/probe.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
746746
let (xform_self_ty, xform_ret_ty) = self.xform_self_ty(item, impl_ty, impl_args);
747747
debug!("xform_self_ty: {:?}, xform_ret_ty: {:?}", xform_self_ty, xform_ret_ty);
748748

749-
// We can't use normalize_associated_types_in as it will pollute the
749+
// We can't use `FnCtxt::normalize` as it will pollute the
750750
// fcx's fulfillment context after this probe is over.
751+
//
751752
// Note: we only normalize `xform_self_ty` here since the normalization
752753
// of the return type can lead to inference results that prohibit
753754
// valid candidates from being found, see issue #85671
755+
//
754756
// FIXME Postponing the normalization of the return type likely only hides a deeper bug,
755757
// which might be caused by the `param_env` itself. The clauses of the `param_env`
756758
// maybe shouldn't include `Param`s, but rather fresh variables or be canonicalized,

compiler/rustc_middle/src/ty/print/pretty.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,12 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
804804
}
805805
} else {
806806
p!(print_def_path(did, args));
807-
p!(" upvar_tys=(");
808-
if !args.as_coroutine().is_valid() {
809-
p!("unavailable");
810-
} else {
811-
self.comma_sep(args.as_coroutine().upvar_tys().iter())?;
812-
}
813-
p!(")");
814-
815-
if args.as_coroutine().is_valid() {
816-
p!(" ", print(args.as_coroutine().witness()));
817-
}
807+
p!(
808+
" upvar_tys=",
809+
print(args.as_coroutine().tupled_upvars_ty()),
810+
" witness=",
811+
print(args.as_coroutine().witness())
812+
);
818813
}
819814

820815
p!("}}")
@@ -868,19 +863,14 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
868863
}
869864
} else {
870865
p!(print_def_path(did, args));
871-
if !args.as_closure().is_valid() {
872-
p!(" closure_args=(unavailable)");
873-
p!(write(" args={}", args.print_as_list()));
874-
} else {
875-
p!(" closure_kind_ty=", print(args.as_closure().kind_ty()));
876-
p!(
877-
" closure_sig_as_fn_ptr_ty=",
878-
print(args.as_closure().sig_as_fn_ptr_ty())
879-
);
880-
p!(" upvar_tys=(");
881-
self.comma_sep(args.as_closure().upvar_tys().iter())?;
882-
p!(")");
883-
}
866+
p!(
867+
" closure_kind_ty=",
868+
print(args.as_closure().kind_ty()),
869+
" closure_sig_as_fn_ptr_ty=",
870+
print(args.as_closure().sig_as_fn_ptr_ty()),
871+
" upvar_tys=",
872+
print(args.as_closure().tupled_upvars_ty())
873+
);
884874
}
885875
p!("}}");
886876
}

compiler/rustc_middle/src/ty/sty.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,15 @@ pub struct ClosureArgs<'tcx> {
242242

243243
/// Struct returned by `split()`.
244244
pub struct ClosureArgsParts<'tcx> {
245+
/// This is the args of the typeck root.
245246
pub parent_args: &'tcx [GenericArg<'tcx>],
247+
/// Represents the maximum calling capability of the closure.
246248
pub closure_kind_ty: Ty<'tcx>,
249+
/// Captures the closure's signature. This closure signature is "tupled", and
250+
/// thus has a peculiar signature of `extern "rust-call" fn((Args, ...)) -> Ty`.
247251
pub closure_sig_as_fn_ptr_ty: Ty<'tcx>,
252+
/// The upvars captured by the closure. Remains an inference variable
253+
/// until the upvar analysis, which happens late in HIR typeck.
248254
pub tupled_upvars_ty: Ty<'tcx>,
249255
}
250256

@@ -277,15 +283,6 @@ impl<'tcx> ClosureArgs<'tcx> {
277283
}
278284
}
279285

280-
/// Returns `true` only if enough of the synthetic types are known to
281-
/// allow using all of the methods on `ClosureArgs` without panicking.
282-
///
283-
/// Used primarily by `ty::print::pretty` to be able to handle closure
284-
/// types that haven't had their synthetic types substituted in.
285-
pub fn is_valid(self) -> bool {
286-
self.args.len() >= 3 && matches!(self.split().tupled_upvars_ty.kind(), Tuple(_))
287-
}
288-
289286
/// Returns the substitutions of the closure's parent.
290287
pub fn parent_args(self) -> &'tcx [GenericArg<'tcx>] {
291288
self.split().parent_args
@@ -296,9 +293,9 @@ impl<'tcx> ClosureArgs<'tcx> {
296293
/// empty iterator is returned.
297294
#[inline]
298295
pub fn upvar_tys(self) -> &'tcx List<Ty<'tcx>> {
299-
match self.tupled_upvars_ty().kind() {
296+
match *self.tupled_upvars_ty().kind() {
300297
TyKind::Error(_) => ty::List::empty(),
301-
TyKind::Tuple(..) => self.tupled_upvars_ty().tuple_fields(),
298+
TyKind::Tuple(tys) => tys,
302299
TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
303300
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
304301
}
@@ -337,10 +334,9 @@ impl<'tcx> ClosureArgs<'tcx> {
337334

338335
/// Extracts the signature from the closure.
339336
pub fn sig(self) -> ty::PolyFnSig<'tcx> {
340-
let ty = self.sig_as_fn_ptr_ty();
341-
match ty.kind() {
342-
ty::FnPtr(sig) => *sig,
343-
_ => bug!("closure_sig_as_fn_ptr_ty is not a fn-ptr: {:?}", ty.kind()),
337+
match *self.sig_as_fn_ptr_ty().kind() {
338+
ty::FnPtr(sig) => sig,
339+
ty => bug!("closure_sig_as_fn_ptr_ty is not a fn-ptr: {ty:?}"),
344340
}
345341
}
346342

@@ -356,11 +352,17 @@ pub struct CoroutineArgs<'tcx> {
356352
}
357353

358354
pub struct CoroutineArgsParts<'tcx> {
355+
/// This is the args of the typeck root.
359356
pub parent_args: &'tcx [GenericArg<'tcx>],
360357
pub resume_ty: Ty<'tcx>,
361358
pub yield_ty: Ty<'tcx>,
362359
pub return_ty: Ty<'tcx>,
360+
/// The interior type of the coroutine.
361+
/// Represents all types that are stored in locals
362+
/// in the coroutine's body.
363363
pub witness: Ty<'tcx>,
364+
/// The upvars captured by the closure. Remains an inference variable
365+
/// until the upvar analysis, which happens late in HIR typeck.
364366
pub tupled_upvars_ty: Ty<'tcx>,
365367
}
366368

@@ -397,15 +399,6 @@ impl<'tcx> CoroutineArgs<'tcx> {
397399
}
398400
}
399401

400-
/// Returns `true` only if enough of the synthetic types are known to
401-
/// allow using all of the methods on `CoroutineArgs` without panicking.
402-
///
403-
/// Used primarily by `ty::print::pretty` to be able to handle coroutine
404-
/// types that haven't had their synthetic types substituted in.
405-
pub fn is_valid(self) -> bool {
406-
self.args.len() >= 5 && matches!(self.split().tupled_upvars_ty.kind(), Tuple(_))
407-
}
408-
409402
/// Returns the substitutions of the coroutine's parent.
410403
pub fn parent_args(self) -> &'tcx [GenericArg<'tcx>] {
411404
self.split().parent_args
@@ -425,9 +418,9 @@ impl<'tcx> CoroutineArgs<'tcx> {
425418
/// empty iterator is returned.
426419
#[inline]
427420
pub fn upvar_tys(self) -> &'tcx List<Ty<'tcx>> {
428-
match self.tupled_upvars_ty().kind() {
421+
match *self.tupled_upvars_ty().kind() {
429422
TyKind::Error(_) => ty::List::empty(),
430-
TyKind::Tuple(..) => self.tupled_upvars_ty().tuple_fields(),
423+
TyKind::Tuple(tys) => tys,
431424
TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
432425
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
433426
}

compiler/rustc_trait_selection/src/solve/normalizes_to/weak_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Computes a normalizes-to (projection) goal for inherent associated types,
22
//! `#![feature(lazy_type_alias)]` and `#![feature(type_alias_impl_trait)]`.
33
//!
4-
//! Since a weak alias is not ambiguous, this just computes the `type_of` of
4+
//! Since a weak alias is never ambiguous, this just computes the `type_of` of
55
//! the alias and registers the where-clauses of the type alias.
66
use rustc_middle::traits::solve::{Certainty, Goal, GoalSource, QueryResult};
77
use rustc_middle::ty;

compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs

+5-34
Original file line numberDiff line numberDiff line change
@@ -232,32 +232,12 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
232232
Ok::<_, NoSolution>(())
233233
})?,
234234

235-
ty::Closure(_, args) => {
236-
if !args.as_closure().is_valid() {
237-
// By the time this code runs, all type variables ought to
238-
// be fully resolved.
239-
240-
tcx.dcx().span_delayed_bug(
241-
span,
242-
format!("upvar_tys for closure not found. Expected capture information for closure {ty}",),
243-
);
244-
return Err(NoSolution);
235+
ty::Closure(_, args) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
236+
for ty in args.as_closure().upvar_tys() {
237+
dtorck_constraint_for_ty_inner(tcx, param_env, span, depth + 1, ty, constraints)?;
245238
}
246-
247-
rustc_data_structures::stack::ensure_sufficient_stack(|| {
248-
for ty in args.as_closure().upvar_tys() {
249-
dtorck_constraint_for_ty_inner(
250-
tcx,
251-
param_env,
252-
span,
253-
depth + 1,
254-
ty,
255-
constraints,
256-
)?;
257-
}
258-
Ok::<_, NoSolution>(())
259-
})?
260-
}
239+
Ok::<_, NoSolution>(())
240+
})?,
261241

262242
ty::Coroutine(_, args) => {
263243
// rust-lang/rust#49918: types can be constructed, stored
@@ -283,15 +263,6 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
283263
// derived from lifetimes attached to the upvars and resume
284264
// argument, and we *do* incorporate those here.
285265
let args = args.as_coroutine();
286-
if !args.is_valid() {
287-
// By the time this code runs, all type variables ought to
288-
// be fully resolved.
289-
tcx.dcx().span_delayed_bug(
290-
span,
291-
format!("upvar_tys for coroutine not found. Expected capture information for coroutine {ty}",),
292-
);
293-
return Err(NoSolution);
294-
}
295266

296267
// While we conservatively assume that all coroutines require drop
297268
// to avoid query cycles during MIR building, we can check the actual

compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ where
141141
infcx: &InferCtxt<'tcx>,
142142
span: Span,
143143
) -> Result<TypeOpOutput<'tcx, Self>, ErrorGuaranteed> {
144+
// In the new trait solver, query type ops are performed locally. This
145+
// is because query type ops currently use the old canonicalizer, and
146+
// that doesn't preserve things like opaques which have been registered
147+
// during MIR typeck. Even after the old canonicalizer is gone, it's
148+
// probably worthwhile just keeping this run-locally logic, since we
149+
// probably don't gain much from caching here given the new solver does
150+
// caching internally.
144151
if infcx.next_trait_solver() {
145152
return Ok(scrape_region_constraints(
146153
infcx,

tests/ui/closures/print/closure-print-generic-trim-off-verbose-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let c1 : () = c;
99
| expected due to this
1010
|
1111
= note: expected unit type `()`
12-
found closure `{mod1::f<T>::{closure#0} closure_args=(unavailable) args=[T, ?8t, extern "rust-call" fn(()), ?7t]}`
12+
found closure `{mod1::f<T>::{closure#0} closure_kind_ty=?8t closure_sig_as_fn_ptr_ty=extern "rust-call" fn(()) upvar_tys=?7t}`
1313
help: use parentheses to call this closure
1414
|
1515
LL | let c1 : () = c();

tests/ui/closures/print/closure-print-generic-verbose-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let c1 : () = c;
99
| expected due to this
1010
|
1111
= note: expected unit type `()`
12-
found closure `{f<T>::{closure#0} closure_args=(unavailable) args=[T, ?8t, extern "rust-call" fn(()), ?7t]}`
12+
found closure `{f<T>::{closure#0} closure_kind_ty=?8t closure_sig_as_fn_ptr_ty=extern "rust-call" fn(()) upvar_tys=?7t}`
1313
help: use parentheses to call this closure
1414
|
1515
LL | let c1 : () = c();

tests/ui/closures/print/closure-print-verbose.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
77
| expected due to this
88
|
99
= note: expected fn pointer `fn(u8) -> u8`
10-
found closure `{main::{closure#0} closure_args=(unavailable) args=[i8, extern "rust-call" fn((u8,)) -> u8, ?4t]}`
10+
found closure `{main::{closure#0} closure_kind_ty=i8 closure_sig_as_fn_ptr_ty=extern "rust-call" fn((u8,)) -> u8 upvar_tys=?4t}`
1111
note: closures can only be coerced to `fn` types if they do not capture any variables
1212
--> $DIR/closure-print-verbose.rs:10:39
1313
|

tests/ui/coroutine/print/coroutine-print-verbose-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: coroutine cannot be shared between threads safely
44
LL | assert_sync(|| {
55
| ^^^^^^^^^^^ coroutine is not `Sync`
66
|
7-
= help: within `{main::{closure#0} upvar_tys=() {main::{closure#0}}}`, the trait `Sync` is not implemented for `NotSync`
7+
= help: within `{main::{closure#0} upvar_tys=() witness={main::{closure#0}}}`, the trait `Sync` is not implemented for `NotSync`
88
note: coroutine is not `Sync` as this value is used across a yield
99
--> $DIR/coroutine-print-verbose-2.rs:20:9
1010
|
@@ -24,7 +24,7 @@ error: coroutine cannot be sent between threads safely
2424
LL | assert_send(|| {
2525
| ^^^^^^^^^^^ coroutine is not `Send`
2626
|
27-
= help: within `{main::{closure#1} upvar_tys=() {main::{closure#1}}}`, the trait `Send` is not implemented for `NotSend`
27+
= help: within `{main::{closure#1} upvar_tys=() witness={main::{closure#1}}}`, the trait `Send` is not implemented for `NotSend`
2828
note: coroutine is not `Send` as this value is used across a yield
2929
--> $DIR/coroutine-print-verbose-2.rs:27:9
3030
|

tests/ui/coroutine/print/coroutine-print-verbose-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | | };
1212
| |_____^ expected `()`, found coroutine
1313
|
1414
= note: expected unit type `()`
15-
found coroutine `{main::{closure#0} upvar_tys=(unavailable)}`
15+
found coroutine `{main::{closure#0} upvar_tys=?4t witness=?6t}`
1616

1717
error: aborting due to 1 previous error
1818

0 commit comments

Comments
 (0)