Skip to content

Commit 0f1efaf

Browse files
committed
Auto merge of rust-lang#130722 - matthiaskrgr:rollup-ocqdu4y, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#122565 (Try to write the panic message with a single `write_all` call) - rust-lang#129550 (Add str.as_str() for easy Deref to string slices) - rust-lang#130659 (Support `char::encode_utf16` in const scenarios.) - rust-lang#130705 (No longer mark RTN as incomplete) - rust-lang#130712 (Don't call `ty::Const::normalize` in error reporting) - rust-lang#130713 (Mark `u8::make_ascii_uppercase` and `u8::make_ascii_lowercase` as const.) - rust-lang#130714 (Introduce `structurally_normalize_const`, use it in `rustc_hir_typeck`) - rust-lang#130715 (Replace calls to `ty::Const::{try_}eval` in mir build/pattern analysis) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6c6d210 + 4217792 commit 0f1efaf

File tree

204 files changed

+609
-766
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+609
-766
lines changed

compiler/rustc_feature/src/unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ declare_features! (
578578
/// be used to describe E or vise-versa.
579579
(unstable, result_ffi_guarantees, "1.80.0", Some(110503)),
580580
/// Allows bounding the return type of AFIT/RPITIT.
581-
(incomplete, return_type_notation, "1.70.0", Some(109417)),
581+
(unstable, return_type_notation, "1.70.0", Some(109417)),
582582
/// Allows `extern "rust-cold"`.
583583
(unstable, rust_cold_cc, "1.63.0", Some(97544)),
584584
/// Allows use of x86 SHA512, SM3 and SM4 target-features and intrinsics

compiler/rustc_hir/src/hir.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1668,10 +1668,17 @@ pub enum ArrayLen<'hir> {
16681668
}
16691669

16701670
impl ArrayLen<'_> {
1671-
pub fn hir_id(&self) -> HirId {
1671+
pub fn span(self) -> Span {
1672+
match self {
1673+
ArrayLen::Infer(arg) => arg.span,
1674+
ArrayLen::Body(body) => body.span(),
1675+
}
1676+
}
1677+
1678+
pub fn hir_id(self) -> HirId {
16721679
match self {
1673-
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(ConstArg { hir_id, .. }) => {
1674-
*hir_id
1680+
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(&ConstArg { hir_id, .. }) => {
1681+
hir_id
16751682
}
16761683
}
16771684
}

compiler/rustc_hir_typeck/src/expr.rs

+35-31
Original file line numberDiff line numberDiff line change
@@ -1491,8 +1491,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14911491
expr: &'tcx hir::Expr<'tcx>,
14921492
) -> Ty<'tcx> {
14931493
let tcx = self.tcx;
1494-
let count = self.lower_array_length(count);
1495-
if let Some(count) = count.try_eval_target_usize(tcx, self.param_env) {
1494+
let count_span = count.span();
1495+
let count = self.try_structurally_resolve_const(count_span, self.lower_array_length(count));
1496+
1497+
if let Some(count) = count.try_to_target_usize(tcx) {
14961498
self.suggest_array_len(expr, count);
14971499
}
14981500

@@ -1520,19 +1522,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15201522
return Ty::new_error(tcx, guar);
15211523
}
15221524

1523-
self.check_repeat_element_needs_copy_bound(element, count, element_ty);
1525+
// If the length is 0, we don't create any elements, so we don't copy any.
1526+
// If the length is 1, we don't copy that one element, we move it. Only check
1527+
// for `Copy` if the length is larger, or unevaluated.
1528+
// FIXME(min_const_generic_exprs): We could perhaps defer this check so that
1529+
// we don't require `<?0t as Tr>::CONST` doesn't unnecessarily require `Copy`.
1530+
if count.try_to_target_usize(tcx).is_none_or(|x| x > 1) {
1531+
self.enforce_repeat_element_needs_copy_bound(element, element_ty);
1532+
}
15241533

15251534
let ty = Ty::new_array_with_const_len(tcx, t, count);
1526-
15271535
self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1528-
15291536
ty
15301537
}
15311538

1532-
fn check_repeat_element_needs_copy_bound(
1539+
/// Requires that `element_ty` is `Copy` (unless it's a const expression itself).
1540+
fn enforce_repeat_element_needs_copy_bound(
15331541
&self,
15341542
element: &hir::Expr<'_>,
1535-
count: ty::Const<'tcx>,
15361543
element_ty: Ty<'tcx>,
15371544
) {
15381545
let tcx = self.tcx;
@@ -1565,27 +1572,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15651572
_ => traits::IsConstable::No,
15661573
};
15671574

1568-
// If the length is 0, we don't create any elements, so we don't copy any. If the length is 1, we
1569-
// don't copy that one element, we move it. Only check for Copy if the length is larger.
1570-
if count.try_eval_target_usize(tcx, self.param_env).is_none_or(|len| len > 1) {
1571-
let lang_item = self.tcx.require_lang_item(LangItem::Copy, None);
1572-
let code = traits::ObligationCauseCode::RepeatElementCopy {
1573-
is_constable,
1574-
elt_type: element_ty,
1575-
elt_span: element.span,
1576-
elt_stmt_span: self
1577-
.tcx
1578-
.hir()
1579-
.parent_iter(element.hir_id)
1580-
.find_map(|(_, node)| match node {
1581-
hir::Node::Item(it) => Some(it.span),
1582-
hir::Node::Stmt(stmt) => Some(stmt.span),
1583-
_ => None,
1584-
})
1585-
.expect("array repeat expressions must be inside an item or statement"),
1586-
};
1587-
self.require_type_meets(element_ty, element.span, code, lang_item);
1588-
}
1575+
let lang_item = self.tcx.require_lang_item(LangItem::Copy, None);
1576+
let code = traits::ObligationCauseCode::RepeatElementCopy {
1577+
is_constable,
1578+
elt_type: element_ty,
1579+
elt_span: element.span,
1580+
elt_stmt_span: self
1581+
.tcx
1582+
.hir()
1583+
.parent_iter(element.hir_id)
1584+
.find_map(|(_, node)| match node {
1585+
hir::Node::Item(it) => Some(it.span),
1586+
hir::Node::Stmt(stmt) => Some(stmt.span),
1587+
_ => None,
1588+
})
1589+
.expect("array repeat expressions must be inside an item or statement"),
1590+
};
1591+
self.require_type_meets(element_ty, element.span, code, lang_item);
15891592
}
15901593

15911594
fn check_expr_tuple(
@@ -2800,9 +2803,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28002803
len: ty::Const<'tcx>,
28012804
) {
28022805
err.span_label(field.span, "unknown field");
2803-
if let (Some(len), Ok(user_index)) =
2804-
(len.try_eval_target_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
2805-
{
2806+
if let (Some(len), Ok(user_index)) = (
2807+
self.try_structurally_resolve_const(base.span, len).try_to_target_usize(self.tcx),
2808+
field.as_str().parse::<u64>(),
2809+
) {
28062810
let help = "instead of using tuple indexing, use array indexing";
28072811
let applicability = if len < user_index {
28082812
Applicability::MachineApplicable

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14771477
}
14781478
}
14791479

1480+
#[instrument(level = "debug", skip(self, sp), ret)]
1481+
pub fn try_structurally_resolve_const(&self, sp: Span, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1482+
// FIXME(min_const_generic_exprs): We could process obligations here if `ct` is a var.
1483+
1484+
if self.next_trait_solver()
1485+
&& let ty::ConstKind::Unevaluated(..) = ct.kind()
1486+
{
1487+
// We need to use a separate variable here as otherwise the temporary for
1488+
// `self.fulfillment_cx.borrow_mut()` is alive in the `Err` branch, resulting
1489+
// in a reentrant borrow, causing an ICE.
1490+
let result = self
1491+
.at(&self.misc(sp), self.param_env)
1492+
.structurally_normalize_const(ct, &mut **self.fulfillment_cx.borrow_mut());
1493+
match result {
1494+
Ok(normalized_ct) => normalized_ct,
1495+
Err(errors) => {
1496+
let guar = self.err_ctxt().report_fulfillment_errors(errors);
1497+
return ty::Const::new_error(self.tcx, guar);
1498+
}
1499+
}
1500+
} else if self.tcx.features().generic_const_exprs {
1501+
ct.normalize(self.tcx, self.param_env)
1502+
} else {
1503+
ct
1504+
}
1505+
}
1506+
14801507
/// Resolves `ty` by a single level if `ty` is a type variable.
14811508
///
14821509
/// When the new solver is enabled, this will also attempt to normalize

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15021502
// Create an dummy type `&[_]` so that both &[] and `&Vec<T>` can coerce to it.
15031503
let dummy_ty = if let ty::Array(elem_ty, size) = peeled.kind()
15041504
&& let ty::Infer(_) = elem_ty.kind()
1505-
&& size.try_eval_target_usize(self.tcx, self.param_env) == Some(0)
1505+
&& self
1506+
.try_structurally_resolve_const(provided_expr.span, *size)
1507+
.try_to_target_usize(self.tcx)
1508+
== Some(0)
15061509
{
15071510
let slice = Ty::new_slice(self.tcx, *elem_ty);
15081511
Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, slice)

compiler/rustc_hir_typeck/src/intrinsicck.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
101101
}
102102
}
103103
Ok(SizeSkeleton::Generic(size)) => {
104-
if let Some(size) = size.try_eval_target_usize(tcx, self.param_env) {
104+
if let Some(size) =
105+
self.try_structurally_resolve_const(span, size).try_to_target_usize(tcx)
106+
{
105107
format!("{size} bytes")
106108
} else {
107109
format!("generic size {size}")

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1721,20 +1721,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17211721
}
17221722
}
17231723

1724-
if item_name.name == sym::as_str && rcvr_ty.peel_refs().is_str() {
1725-
let msg = "remove this method call";
1726-
let mut fallback_span = true;
1727-
if let SelfSource::MethodCall(expr) = source {
1728-
let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));
1729-
if let Some(span) = call_expr.span.trim_start(expr.span) {
1730-
err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
1731-
fallback_span = false;
1732-
}
1733-
}
1734-
if fallback_span {
1735-
err.span_label(span, msg);
1736-
}
1737-
} else if let Some(similar_candidate) = similar_candidate {
1724+
if let Some(similar_candidate) = similar_candidate {
17381725
// Don't emit a suggestion if we found an actual method
17391726
// that had unsatisfied trait bounds
17401727
if unsatisfied_predicates.is_empty()

compiler/rustc_hir_typeck/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24122412
len: ty::Const<'tcx>,
24132413
min_len: u64,
24142414
) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
2415-
let len = len.try_eval_target_usize(self.tcx, self.param_env);
2415+
let len = self.try_structurally_resolve_const(span, len).try_to_target_usize(self.tcx);
24162416

24172417
let guar = if let Some(len) = len {
24182418
// Now we know the length...

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5757
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
5858
}
5959
ExprKind::Repeat { value, count } => {
60-
if Some(0) == count.try_eval_target_usize(this.tcx, this.param_env) {
60+
if Some(0) == count.try_to_target_usize(this.tcx) {
6161
this.build_zero_repeat(block, value, scope, source_info)
6262
} else {
6363
let value_operand = unpack!(

compiler/rustc_mir_build/src/build/matches/match_pair.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4242
let tcx = self.tcx;
4343
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
4444
match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
45-
ty::Array(_, length) => (length.eval_target_usize(tcx, self.param_env), true),
45+
ty::Array(_, length) => (
46+
length
47+
.try_to_target_usize(tcx)
48+
.expect("expected len of array pat to be definite"),
49+
true,
50+
),
4651
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
4752
}
4853
} else {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
441441
ty::Slice(..) => PatKind::Slice { prefix, slice, suffix },
442442
// Fixed-length array, `[T; len]`.
443443
ty::Array(_, len) => {
444-
let len = len.eval_target_usize(self.tcx, self.param_env);
444+
let len = len
445+
.try_to_target_usize(self.tcx)
446+
.expect("expected len of array pat to be definite");
445447
assert!(len >= prefix.len() as u64 + suffix.len() as u64);
446448
PatKind::Array { prefix, slice, suffix }
447449
}

compiler/rustc_pattern_analysis/src/rustc.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
352352
ty::Array(sub_ty, len) => {
353353
// We treat arrays of a constant but unknown length like slices.
354354
ConstructorSet::Slice {
355-
array_len: len.try_eval_target_usize(cx.tcx, cx.param_env).map(|l| l as usize),
355+
array_len: len.try_to_target_usize(cx.tcx).map(|l| l as usize),
356356
subtype_is_empty: cx.is_uninhabited(*sub_ty),
357357
}
358358
}
@@ -685,9 +685,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
685685
}
686686
PatKind::Array { prefix, slice, suffix } | PatKind::Slice { prefix, slice, suffix } => {
687687
let array_len = match ty.kind() {
688-
ty::Array(_, length) => {
689-
Some(length.eval_target_usize(cx.tcx, cx.param_env) as usize)
690-
}
688+
ty::Array(_, length) => Some(
689+
length
690+
.try_to_target_usize(cx.tcx)
691+
.expect("expected len of array pat to be definite")
692+
as usize,
693+
),
691694
ty::Slice(_) => None,
692695
_ => span_bug!(pat.span, "bad ty {} for slice pattern", ty.inner()),
693696
};

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::traits::select::OverflowError;
1717
use rustc_middle::traits::SignatureMismatchData;
1818
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1919
use rustc_middle::ty::error::{ExpectedFound, TypeError};
20-
use rustc_middle::ty::fold::{BottomUpFolder, TypeFolder, TypeSuperFoldable};
20+
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
2121
use rustc_middle::ty::print::{
2222
with_forced_trimmed_paths, FmtPrinter, Print, PrintTraitPredicateExt as _,
2323
PrintTraitRefExt as _,
@@ -1794,22 +1794,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17941794
return false;
17951795
}
17961796

1797-
let cand = self.resolve_vars_if_possible(impl_trait_ref).fold_with(
1798-
&mut BottomUpFolder {
1799-
tcx: self.tcx,
1800-
ty_op: |ty| ty,
1801-
lt_op: |lt| lt,
1802-
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
1803-
},
1804-
);
1805-
if cand.references_error() {
1797+
let impl_trait_ref = self.resolve_vars_if_possible(impl_trait_ref);
1798+
if impl_trait_ref.references_error() {
18061799
return false;
18071800
}
18081801
err.highlighted_help(vec![
1809-
StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
1802+
StringPart::normal(format!(
1803+
"the trait `{}` ",
1804+
impl_trait_ref.print_trait_sugared()
1805+
)),
18101806
StringPart::highlighted("is"),
18111807
StringPart::normal(" implemented for `"),
1812-
StringPart::highlighted(cand.self_ty().to_string()),
1808+
StringPart::highlighted(impl_trait_ref.self_ty().to_string()),
18131809
StringPart::normal("`"),
18141810
]);
18151811

@@ -1921,15 +1917,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19211917
let mut impl_candidates: Vec<_> = impl_candidates
19221918
.iter()
19231919
.cloned()
1920+
.filter(|cand| !cand.trait_ref.references_error())
19241921
.map(|mut cand| {
1925-
// Fold the consts so that they shows up as, e.g., `10`
1926-
// instead of `core::::array::{impl#30}::{constant#0}`.
1927-
cand.trait_ref = cand.trait_ref.fold_with(&mut BottomUpFolder {
1928-
tcx: self.tcx,
1929-
ty_op: |ty| ty,
1930-
lt_op: |lt| lt,
1931-
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
1932-
});
1922+
// Normalize the trait ref in its *own* param-env so
1923+
// that consts are folded and any trivial projections
1924+
// are normalized.
1925+
cand.trait_ref = self
1926+
.tcx
1927+
.try_normalize_erasing_regions(
1928+
self.tcx.param_env(cand.impl_def_id),
1929+
cand.trait_ref,
1930+
)
1931+
.unwrap_or(cand.trait_ref);
19331932
cand
19341933
})
19351934
.collect();

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4621,7 +4621,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
46214621
format!("&{}{ty}", mutability.prefix_str())
46224622
}
46234623
}
4624-
ty::Array(ty, len) if let Some(len) = len.try_eval_target_usize(tcx, param_env) => {
4624+
ty::Array(ty, len) if let Some(len) = len.try_to_target_usize(tcx) => {
46254625
if len == 0 {
46264626
"[]".to_string()
46274627
} else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {

compiler/rustc_trait_selection/src/traits/structural_normalize.rs

+40
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,44 @@ impl<'tcx> At<'_, 'tcx> {
4646
Ok(self.normalize(ty).into_value_registering_obligations(self.infcx, fulfill_cx))
4747
}
4848
}
49+
50+
fn structurally_normalize_const<E: 'tcx>(
51+
&self,
52+
ct: ty::Const<'tcx>,
53+
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
54+
) -> Result<ty::Const<'tcx>, Vec<E>> {
55+
assert!(!ct.is_ct_infer(), "should have resolved vars before calling");
56+
57+
if self.infcx.next_trait_solver() {
58+
let ty::ConstKind::Unevaluated(..) = ct.kind() else {
59+
return Ok(ct);
60+
};
61+
62+
let new_infer_ct = self.infcx.next_const_var(self.cause.span);
63+
64+
// We simply emit an `alias-eq` goal here, since that will take care of
65+
// normalizing the LHS of the projection until it is a rigid projection
66+
// (or a not-yet-defined opaque in scope).
67+
let obligation = Obligation::new(
68+
self.infcx.tcx,
69+
self.cause.clone(),
70+
self.param_env,
71+
ty::PredicateKind::AliasRelate(
72+
ct.into(),
73+
new_infer_ct.into(),
74+
ty::AliasRelationDirection::Equate,
75+
),
76+
);
77+
78+
fulfill_cx.register_predicate_obligation(self.infcx, obligation);
79+
let errors = fulfill_cx.select_where_possible(self.infcx);
80+
if !errors.is_empty() {
81+
return Err(errors);
82+
}
83+
84+
Ok(self.infcx.resolve_vars_if_possible(new_infer_ct))
85+
} else {
86+
Ok(self.normalize(ct).into_value_registering_obligations(self.infcx, fulfill_cx))
87+
}
88+
}
4989
}

0 commit comments

Comments
 (0)