Skip to content

Commit e08d569

Browse files
committed
Auto merge of #94148 - matthiaskrgr:rollup-jgea68f, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #92902 (Improve the documentation of drain members) - #93658 (Stabilize `#[cfg(panic = "...")]`) - #93954 (rustdoc-json: buffer output) - #93979 (Add debug assertions to validate NUL terminator in c strings) - #93990 (pre #89862 cleanup) - #94006 (Use a `Field` in `ConstraintCategory::ClosureUpvar`) - #94086 (Fix ScalarInt to char conversion) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents cb4ee81 + 5a083db commit e08d569

File tree

34 files changed

+256
-214
lines changed

34 files changed

+256
-214
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_infer::infer::{
55
error_reporting::nice_region_error::NiceRegionError,
66
error_reporting::unexpected_hidden_region_diagnostic, NllRegionVariableOrigin,
77
};
8+
use rustc_middle::hir::place::PlaceBase;
89
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
910
use rustc_middle::ty::subst::{InternalSubsts, Subst};
1011
use rustc_middle::ty::{self, RegionVid, Ty};
@@ -421,17 +422,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
421422

422423
diag.span_label(*span, message);
423424

424-
// FIXME(project-rfc-2229#48): This should store a captured_place not a hir id
425-
if let ReturnConstraint::ClosureUpvar(upvar) = kind {
425+
if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
426426
let def_id = match self.regioncx.universal_regions().defining_ty {
427427
DefiningTy::Closure(def_id, _) => def_id,
428428
ty => bug!("unexpected DefiningTy {:?}", ty),
429429
};
430430

431-
let upvar_def_span = self.infcx.tcx.hir().span(upvar);
432-
let upvar_span = self.infcx.tcx.upvars_mentioned(def_id).unwrap()[&upvar].span;
433-
diag.span_label(upvar_def_span, "variable defined here");
434-
diag.span_label(upvar_span, "variable captured here");
431+
let captured_place = &self.upvars[upvar_field.index()].place;
432+
let defined_hir = match captured_place.place.base {
433+
PlaceBase::Local(hirid) => Some(hirid),
434+
PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
435+
_ => None,
436+
};
437+
438+
if defined_hir.is_some() {
439+
let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
440+
let upvar_def_span = self.infcx.tcx.hir().span(defined_hir.unwrap());
441+
let upvar_span = upvars_map.get(&defined_hir.unwrap()).unwrap().span;
442+
diag.span_label(upvar_def_span, "variable defined here");
443+
diag.span_label(upvar_span, "variable captured here");
444+
}
435445
}
436446

437447
if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {

compiler/rustc_borrowck/src/type_check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2530,9 +2530,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25302530
body,
25312531
);
25322532
let category = if let Some(field) = field {
2533-
let var_hir_id = self.borrowck_context.upvars[field.index()].place.get_root_variable();
2534-
// FIXME(project-rfc-2229#8): Use Place for better diagnostics
2535-
ConstraintCategory::ClosureUpvar(var_hir_id)
2533+
ConstraintCategory::ClosureUpvar(field)
25362534
} else {
25372535
ConstraintCategory::Boring
25382536
};

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ declare_features! (
7070
(accepted, cfg_attr_multi, "1.33.0", Some(54881), None),
7171
/// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests.
7272
(accepted, cfg_doctest, "1.40.0", Some(62210), None),
73+
/// Enables `#[cfg(panic = "...")]` config key.
74+
(accepted, cfg_panic, "1.60.0", Some(77443), None),
7375
/// Allows `cfg(target_feature = "...")`.
7476
(accepted, cfg_target_feature, "1.27.0", Some(29717), None),
7577
/// Allows `cfg(target_vendor = "...")`.

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,6 @@ declare_features! (
306306
(active, c_variadic, "1.34.0", Some(44930), None),
307307
/// Allows capturing disjoint fields in a closure/generator (RFC 2229).
308308
(incomplete, capture_disjoint_fields, "1.49.0", Some(53488), None),
309-
/// Enables `#[cfg(panic = "...")]` config key.
310-
(active, cfg_panic, "1.49.0", Some(77443), None),
311309
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
312310
(active, cfg_sanitize, "1.41.0", Some(39699), None),
313311
/// Allows `cfg(target_abi = "...")`.

compiler/rustc_feature/src/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const GATED_CFGS: &[GatedCfg] = &[
3434
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3535
(sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
3636
(sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
37-
(sym::panic, sym::cfg_panic, cfg_fn!(cfg_panic)),
3837
];
3938

4039
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
497497
let ty_to_string = |ty: Ty<'tcx>| -> String {
498498
let mut s = String::new();
499499
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
500-
let mut inner = self.inner.borrow_mut();
501-
let ty_vars = inner.type_variables();
502-
let getter = move |ty_vid| {
503-
let var_origin = ty_vars.var_origin(ty_vid);
504-
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = var_origin.kind {
500+
let ty_getter = move |ty_vid| {
501+
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) =
502+
self.inner.borrow_mut().type_variables().var_origin(ty_vid).kind
503+
{
504+
Some(name.to_string())
505+
} else {
506+
None
507+
}
508+
};
509+
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
510+
let const_getter = move |ct_vid| {
511+
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) = self
512+
.inner
513+
.borrow_mut()
514+
.const_unification_table()
515+
.probe_value(ct_vid)
516+
.origin
517+
.kind
518+
{
505519
return Some(name.to_string());
520+
} else {
521+
None
506522
}
507-
None
508523
};
509-
printer.name_resolver = Some(Box::new(&getter));
524+
printer.const_infer_name_resolver = Some(Box::new(const_getter));
525+
510526
let _ = if let ty::FnDef(..) = ty.kind() {
511527
// We don't want the regular output for `fn`s because it includes its path in
512528
// invalid pseudo-syntax, we want the `fn`-pointer output instead.

compiler/rustc_middle/src/mir/query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub enum ConstraintCategory {
341341
/// like `Foo { field: my_val }`)
342342
Usage,
343343
OpaqueType,
344-
ClosureUpvar(hir::HirId),
344+
ClosureUpvar(Field),
345345

346346
/// A constraint from a user-written predicate
347347
/// with the provided span, written on the item
@@ -363,7 +363,7 @@ pub enum ConstraintCategory {
363363
#[derive(TyEncodable, TyDecodable, HashStable)]
364364
pub enum ReturnConstraint {
365365
Normal,
366-
ClosureUpvar(hir::HirId),
366+
ClosureUpvar(Field),
367367
}
368368

369369
/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing

compiler/rustc_middle/src/ty/consts/int.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,22 @@ impl From<char> for ScalarInt {
294294
}
295295
}
296296

297+
/// Error returned when a conversion from ScalarInt to char fails.
298+
#[derive(Debug)]
299+
pub struct CharTryFromScalarInt;
300+
297301
impl TryFrom<ScalarInt> for char {
298-
type Error = Size;
302+
type Error = CharTryFromScalarInt;
303+
299304
#[inline]
300-
fn try_from(int: ScalarInt) -> Result<Self, Size> {
301-
int.to_bits(Size::from_bytes(std::mem::size_of::<char>()))
302-
.map(|u| char::from_u32(u.try_into().unwrap()).unwrap())
305+
fn try_from(int: ScalarInt) -> Result<Self, Self::Error> {
306+
let Ok(bits) = int.to_bits(Size::from_bytes(std::mem::size_of::<char>())) else {
307+
return Err(CharTryFromScalarInt);
308+
};
309+
match char::from_u32(bits.try_into().unwrap()) {
310+
Some(c) => Ok(c),
311+
None => Err(CharTryFromScalarInt),
312+
}
303313
}
304314
}
305315

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ pub trait PrettyPrinter<'tcx>:
606606
ty::Infer(infer_ty) => {
607607
let verbose = self.tcx().sess.verbose();
608608
if let ty::TyVar(ty_vid) = infer_ty {
609-
if let Some(name) = self.infer_ty_name(ty_vid) {
609+
if let Some(name) = self.ty_infer_name(ty_vid) {
610610
p!(write("{}", name))
611611
} else {
612612
if verbose {
@@ -1015,7 +1015,11 @@ pub trait PrettyPrinter<'tcx>:
10151015
}
10161016
}
10171017

1018-
fn infer_ty_name(&self, _: ty::TyVid) -> Option<String> {
1018+
fn ty_infer_name(&self, _: ty::TyVid) -> Option<String> {
1019+
None
1020+
}
1021+
1022+
fn const_infer_name(&self, _: ty::ConstVid<'tcx>) -> Option<String> {
10191023
None
10201024
}
10211025

@@ -1203,7 +1207,14 @@ pub trait PrettyPrinter<'tcx>:
12031207
}
12041208
}
12051209
}
1206-
ty::ConstKind::Infer(..) => print_underscore!(),
1210+
ty::ConstKind::Infer(infer_ct) => {
1211+
match infer_ct {
1212+
ty::InferConst::Var(ct_vid)
1213+
if let Some(name) = self.const_infer_name(ct_vid) =>
1214+
p!(write("{}", name)),
1215+
_ => print_underscore!(),
1216+
}
1217+
}
12071218
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
12081219
ty::ConstKind::Value(value) => {
12091220
return self.pretty_print_const_value(value, ct.ty(), print_ty);
@@ -1559,7 +1570,8 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
15591570

15601571
pub region_highlight_mode: RegionHighlightMode<'tcx>,
15611572

1562-
pub name_resolver: Option<Box<&'a dyn Fn(ty::TyVid) -> Option<String>>>,
1573+
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<String> + 'a>>,
1574+
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<String> + 'a>>,
15631575
}
15641576

15651577
impl<'a, 'tcx, F> Deref for FmtPrinter<'a, 'tcx, F> {
@@ -1588,7 +1600,8 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
15881600
binder_depth: 0,
15891601
printed_type_count: 0,
15901602
region_highlight_mode: RegionHighlightMode::new(tcx),
1591-
name_resolver: None,
1603+
ty_infer_name_resolver: None,
1604+
const_infer_name_resolver: None,
15921605
}))
15931606
}
15941607
}
@@ -1843,8 +1856,12 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
18431856
}
18441857

18451858
impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1846-
fn infer_ty_name(&self, id: ty::TyVid) -> Option<String> {
1847-
self.0.name_resolver.as_ref().and_then(|func| func(id))
1859+
fn ty_infer_name(&self, id: ty::TyVid) -> Option<String> {
1860+
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
1861+
}
1862+
1863+
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<String> {
1864+
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
18481865
}
18491866

18501867
fn print_value_path(

compiler/rustc_middle/src/ty/util.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_data_structures::intern::Interned;
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1818
use rustc_errors::ErrorReported;
1919
use rustc_hir as hir;
20-
use rustc_hir::def::DefKind;
20+
use rustc_hir::def::{CtorOf, DefKind, Res};
2121
use rustc_hir::def_id::DefId;
2222
use rustc_macros::HashStable;
2323
use rustc_query_system::ich::NodeIdHashingMode;
@@ -146,6 +146,37 @@ impl<'tcx> TyCtxt<'tcx> {
146146
hasher.finish()
147147
}
148148

149+
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
150+
match res {
151+
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
152+
Some(self.parent(def_id).and_then(|def_id| self.parent(def_id)).unwrap())
153+
}
154+
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
155+
Some(self.parent(def_id).unwrap())
156+
}
157+
// Other `DefKind`s don't have generics and would ICE when calling
158+
// `generics_of`.
159+
Res::Def(
160+
DefKind::Struct
161+
| DefKind::Union
162+
| DefKind::Enum
163+
| DefKind::Trait
164+
| DefKind::OpaqueTy
165+
| DefKind::TyAlias
166+
| DefKind::ForeignTy
167+
| DefKind::TraitAlias
168+
| DefKind::AssocTy
169+
| DefKind::Fn
170+
| DefKind::AssocFn
171+
| DefKind::AssocConst
172+
| DefKind::Impl,
173+
def_id,
174+
) => Some(def_id),
175+
Res::Err => None,
176+
_ => None,
177+
}
178+
}
179+
149180
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
150181
if let ty::Adt(def, substs) = *ty.kind() {
151182
for field in def.all_fields() {

compiler/rustc_typeck/src/collect/type_of.rs

+5-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use rustc_errors::{Applicability, ErrorReported, StashKey};
22
use rustc_hir as hir;
3-
use rustc_hir::def::CtorOf;
4-
use rustc_hir::def::{DefKind, Res};
3+
use rustc_hir::def::Res;
54
use rustc_hir::def_id::{DefId, LocalDefId};
65
use rustc_hir::intravisit;
76
use rustc_hir::intravisit::Visitor;
87
use rustc_hir::{HirId, Node};
98
use rustc_middle::hir::nested_filter;
109
use rustc_middle::ty::subst::InternalSubsts;
1110
use rustc_middle::ty::util::IntTypeExt;
12-
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable, TypeFolder};
11+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder};
1312
use rustc_span::symbol::Ident;
1413
use rustc_span::{Span, DUMMY_SP};
1514

@@ -198,38 +197,9 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
198197
// Try to use the segment resolution if it is valid, otherwise we
199198
// default to the path resolution.
200199
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
201-
let generics = match res {
202-
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx
203-
.generics_of(tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap()),
204-
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
205-
tcx.generics_of(tcx.parent(def_id).unwrap())
206-
}
207-
// Other `DefKind`s don't have generics and would ICE when calling
208-
// `generics_of`.
209-
Res::Def(
210-
DefKind::Struct
211-
| DefKind::Union
212-
| DefKind::Enum
213-
| DefKind::Trait
214-
| DefKind::OpaqueTy
215-
| DefKind::TyAlias
216-
| DefKind::ForeignTy
217-
| DefKind::TraitAlias
218-
| DefKind::AssocTy
219-
| DefKind::Fn
220-
| DefKind::AssocFn
221-
| DefKind::AssocConst
222-
| DefKind::Impl,
223-
def_id,
224-
) => tcx.generics_of(def_id),
225-
Res::Err => {
226-
tcx.sess.delay_span_bug(tcx.def_span(def_id), "anon const with Res::Err");
227-
return None;
228-
}
229-
_ => {
230-
// If the user tries to specify generics on a type that does not take them,
231-
// e.g. `usize<T>`, we may hit this branch, in which case we treat it as if
232-
// no arguments have been passed. An error should already have been emitted.
200+
let generics = match tcx.res_generics_def_id(res) {
201+
Some(def_id) => tcx.generics_of(def_id),
202+
None => {
233203
tcx.sess.delay_span_bug(
234204
tcx.def_span(def_id),
235205
&format!("unexpected anon const res {:?} in path: {:?}", res, path),

library/alloc/src/collections/binary_heap.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,12 @@ impl<T: Ord> BinaryHeap<T> {
746746
self.rebuild_tail(start);
747747
}
748748

749-
/// Returns an iterator which retrieves elements in heap order.
750-
/// The retrieved elements are removed from the original heap.
751-
/// The remaining elements will be removed on drop in heap order.
749+
/// Clears the binary heap, returning an iterator over the removed elements
750+
/// in heap order. If the iterator is dropped before being fully consumed,
751+
/// it drops the remaining elements in heap order.
752+
///
753+
/// The returned iterator keeps a mutable borrow on the heap to optimize
754+
/// its implementation.
752755
///
753756
/// Note:
754757
/// * `.drain_sorted()` is *O*(*n* \* log(*n*)); much slower than `.drain()`.
@@ -1158,9 +1161,12 @@ impl<T> BinaryHeap<T> {
11581161
self.len() == 0
11591162
}
11601163

1161-
/// Clears the binary heap, returning an iterator over the removed elements.
1164+
/// Clears the binary heap, returning an iterator over the removed elements
1165+
/// in arbitrary order. If the iterator is dropped before being fully
1166+
/// consumed, it drops the remaining elements in arbitrary order.
11621167
///
1163-
/// The elements are removed in arbitrary order.
1168+
/// The returned iterator keeps a mutable borrow on the heap to optimize
1169+
/// its implementation.
11641170
///
11651171
/// # Examples
11661172
///

0 commit comments

Comments
 (0)