Skip to content

Commit dc7a676

Browse files
committed
Auto merge of #108096 - matthiaskrgr:rollup-ncexzf6, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - #107034 (Migrating rustc_infer to session diagnostics (part 4)) - #107972 (Fix unintentional UB in ui tests) - #108010 (Make `InferCtxt::can_eq` and `InferCtxt::can_sub` return booleans) - #108021 (make x look for x.py if shell script does not exist) - #108047 (Use `target` instead of `machine` for mir interpreter integer handling.) - #108049 (Don't suggest `#[doc(hidden)]` trait methods with matching return type) - #108066 (Better names for illegal impl trait positions) - #108076 (rustdoc: Use more let chain) - #108088 (clarify correctness of `black_box`) - #108094 (Demonstrate I/O in File examples) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents c528357 + 5547101 commit dc7a676

File tree

109 files changed

+1121
-779
lines changed

Some content is hidden

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

109 files changed

+1121
-779
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
139139
ExprKind::Cast(expr, ty) => {
140140
let expr = self.lower_expr(expr);
141141
let ty =
142-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
142+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
143143
hir::ExprKind::Cast(expr, ty)
144144
}
145145
ExprKind::Type(expr, ty) => {
146146
let expr = self.lower_expr(expr);
147147
let ty =
148-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
148+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
149149
hir::ExprKind::Type(expr, ty)
150150
}
151151
ExprKind::AddrOf(k, m, ohs) => {

compiler/rustc_ast_lowering/src/item.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
378378
)
379379
});
380380

381-
let lowered_ty = this
382-
.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
381+
let lowered_ty = this.lower_ty(
382+
ty,
383+
&ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
384+
);
383385

384386
(trait_ref, lowered_ty)
385387
});
@@ -458,7 +460,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
458460
span: Span,
459461
body: Option<&Expr>,
460462
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
461-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
463+
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
462464
(ty, self.lower_const_body(span, body))
463465
}
464466

@@ -608,8 +610,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
608610
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
609611
}
610612
ForeignItemKind::Static(t, m, _) => {
611-
let ty =
612-
self.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
613+
let ty = self
614+
.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
613615
hir::ForeignItemKind::Static(ty, *m)
614616
}
615617
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
@@ -679,11 +681,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
679681
qself,
680682
path,
681683
ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
682-
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
684+
&ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy),
683685
);
684686
self.arena.alloc(t)
685687
} else {
686-
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
688+
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy))
687689
};
688690
let hir_id = self.lower_node_id(f.id);
689691
self.lower_attrs(hir_id, &f.attrs);
@@ -708,7 +710,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
708710

709711
let (generics, kind, has_default) = match &i.kind {
710712
AssocItemKind::Const(_, ty, default) => {
711-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
713+
let ty =
714+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
712715
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
713716
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
714717
}
@@ -746,7 +749,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
746749
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
747750
|this| {
748751
let ty = ty.as_ref().map(|x| {
749-
this.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
752+
this.lower_ty(
753+
x,
754+
&ImplTraitContext::Disallowed(ImplTraitPosition::AssocTy),
755+
)
750756
});
751757
hir::TraitItemKind::Type(
752758
this.lower_param_bounds(
@@ -805,7 +811,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
805811

806812
let (generics, kind) = match &i.kind {
807813
AssocItemKind::Const(_, ty, expr) => {
808-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
814+
let ty =
815+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
809816
(
810817
hir::Generics::empty(),
811818
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
@@ -1441,7 +1448,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14411448
hir_id: self.next_id(),
14421449
bound_generic_params: self.lower_generic_params(bound_generic_params),
14431450
bounded_ty: self
1444-
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
1451+
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14451452
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
14461453
self.lower_param_bound(
14471454
bound,
@@ -1465,9 +1472,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14651472
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
14661473
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
14671474
lhs_ty: self
1468-
.lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
1475+
.lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14691476
rhs_ty: self
1470-
.lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
1477+
.lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14711478
span: self.lower_span(*span),
14721479
})
14731480
}

compiler/rustc_ast_lowering/src/lib.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ enum ImplTraitContext {
253253
enum ImplTraitPosition {
254254
Path,
255255
Variable,
256-
Type,
257256
Trait,
258257
AsyncBlock,
259258
Bound,
@@ -270,14 +269,20 @@ enum ImplTraitPosition {
270269
FnTraitReturn,
271270
TraitReturn,
272271
ImplReturn,
272+
GenericDefault,
273+
ConstTy,
274+
StaticTy,
275+
AssocTy,
276+
FieldTy,
277+
Cast,
278+
ImplSelf,
273279
}
274280

275281
impl std::fmt::Display for ImplTraitPosition {
276282
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277283
let name = match self {
278284
ImplTraitPosition::Path => "path",
279285
ImplTraitPosition::Variable => "variable binding",
280-
ImplTraitPosition::Type => "type",
281286
ImplTraitPosition::Trait => "trait",
282287
ImplTraitPosition::AsyncBlock => "async block",
283288
ImplTraitPosition::Bound => "bound",
@@ -294,6 +299,13 @@ impl std::fmt::Display for ImplTraitPosition {
294299
ImplTraitPosition::FnTraitReturn => "`Fn` trait return",
295300
ImplTraitPosition::TraitReturn => "trait method return",
296301
ImplTraitPosition::ImplReturn => "`impl` method return",
302+
ImplTraitPosition::GenericDefault => "generic parameter default",
303+
ImplTraitPosition::ConstTy => "const type",
304+
ImplTraitPosition::StaticTy => "static type",
305+
ImplTraitPosition::AssocTy => "associated type",
306+
ImplTraitPosition::FieldTy => "field type",
307+
ImplTraitPosition::Cast => "cast type",
308+
ImplTraitPosition::ImplSelf => "impl header",
297309
};
298310

299311
write!(f, "{name}")
@@ -2166,15 +2178,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21662178
GenericParamKind::Type { default, .. } => {
21672179
let kind = hir::GenericParamKind::Type {
21682180
default: default.as_ref().map(|x| {
2169-
self.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
2181+
self.lower_ty(
2182+
x,
2183+
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
2184+
)
21702185
}),
21712186
synthetic: false,
21722187
};
21732188

21742189
(hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
21752190
}
21762191
GenericParamKind::Const { ty, kw_span: _, default } => {
2177-
let ty = self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
2192+
let ty = self.lower_ty(
2193+
&ty,
2194+
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
2195+
);
21782196
let default = default.as_ref().map(|def| self.lower_anon_const(def));
21792197
(
21802198
hir::ParamName::Plain(self.lower_ident(param.ident)),

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11441144
LateBoundRegionConversionTime::FnCall,
11451145
tcx.fn_sig(method_did).subst(tcx, method_substs).input(0),
11461146
)
1147-
&& infcx.can_eq(self.param_env, ty, self_ty).is_ok()
1147+
&& infcx.can_eq(self.param_env, ty, self_ty)
11481148
{
11491149
err.span_suggestion_verbose(
11501150
fn_call_span.shrink_to_lo(),

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub(super) fn op_to_const<'tcx>(
186186
0,
187187
),
188188
};
189-
let len = b.to_machine_usize(ecx).unwrap();
189+
let len = b.to_target_usize(ecx).unwrap();
190190
let start = start.try_into().unwrap();
191191
let len: usize = len.try_into().unwrap();
192192
ConstValue::Slice { data, start, end: start + len }

compiler/rustc_const_eval/src/const_eval/machine.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
244244
assert_eq!(args.len(), 2);
245245

246246
let ptr = self.read_pointer(&args[0])?;
247-
let target_align = self.read_scalar(&args[1])?.to_machine_usize(self)?;
247+
let target_align = self.read_scalar(&args[1])?.to_target_usize(self)?;
248248

249249
if !target_align.is_power_of_two() {
250250
throw_ub_format!("`align_offset` called with non-power-of-two align: {}", target_align);
@@ -276,7 +276,7 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
276276
Ok(ControlFlow::Break(()))
277277
} else {
278278
// Not alignable in const, return `usize::MAX`.
279-
let usize_max = Scalar::from_machine_usize(self.machine_usize_max(), self);
279+
let usize_max = Scalar::from_target_usize(self.target_usize_max(), self);
280280
self.write_scalar(usize_max, dest)?;
281281
self.return_to_block(ret)?;
282282
Ok(ControlFlow::Break(()))
@@ -470,8 +470,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
470470
ecx.write_scalar(Scalar::from_u8(cmp), dest)?;
471471
}
472472
sym::const_allocate => {
473-
let size = ecx.read_scalar(&args[0])?.to_machine_usize(ecx)?;
474-
let align = ecx.read_scalar(&args[1])?.to_machine_usize(ecx)?;
473+
let size = ecx.read_scalar(&args[0])?.to_target_usize(ecx)?;
474+
let align = ecx.read_scalar(&args[1])?.to_target_usize(ecx)?;
475475

476476
let align = match Align::from_bytes(align) {
477477
Ok(a) => a,
@@ -487,8 +487,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
487487
}
488488
sym::const_deallocate => {
489489
let ptr = ecx.read_pointer(&args[0])?;
490-
let size = ecx.read_scalar(&args[1])?.to_machine_usize(ecx)?;
491-
let align = ecx.read_scalar(&args[2])?.to_machine_usize(ecx)?;
490+
let size = ecx.read_scalar(&args[1])?.to_target_usize(ecx)?;
491+
let align = ecx.read_scalar(&args[2])?.to_target_usize(ecx)?;
492492

493493
let size = Size::from_bytes(size);
494494
let align = match Align::from_bytes(align) {

compiler/rustc_const_eval/src/const_eval/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub(crate) fn deref_mir_constant<'tcx>(
155155
// In case of unsized types, figure out the real type behind.
156156
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
157157
ty::Str => bug!("there's no sized equivalent of a `str`"),
158-
ty::Slice(elem_ty) => tcx.mk_array(*elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
158+
ty::Slice(elem_ty) => tcx.mk_array(*elem_ty, scalar.to_target_usize(&tcx).unwrap()),
159159
_ => bug!(
160160
"type {} should not have metadata, but had {:?}",
161161
mplace.layout.ty,

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn create_pointee_place<'tcx>(
239239
MPlaceTy::from_aligned_ptr_with_meta(
240240
ptr.into(),
241241
layout,
242-
MemPlaceMeta::Meta(Scalar::from_machine_usize(num_elems as u64, &tcx)),
242+
MemPlaceMeta::Meta(Scalar::from_target_usize(num_elems as u64, &tcx)),
243243
)
244244
} else {
245245
create_mplace_from_layout(ecx, ty)
@@ -355,7 +355,7 @@ fn valtree_into_mplace<'tcx>(
355355
let imm = match inner_ty.kind() {
356356
ty::Slice(_) | ty::Str => {
357357
let len = valtree.unwrap_branch().len();
358-
let len_scalar = Scalar::from_machine_usize(len as u64, &tcx);
358+
let len_scalar = Scalar::from_target_usize(len as u64, &tcx);
359359

360360
Immediate::ScalarPair(
361361
Scalar::from_maybe_pointer((*pointee_place).ptr, &tcx),
@@ -426,7 +426,7 @@ fn valtree_into_mplace<'tcx>(
426426
place
427427
.offset_with_meta(
428428
offset,
429-
MemPlaceMeta::Meta(Scalar::from_machine_usize(
429+
MemPlaceMeta::Meta(Scalar::from_target_usize(
430430
num_elems as u64,
431431
&tcx,
432432
)),

compiler/rustc_const_eval/src/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
231231
// First cast to usize.
232232
let scalar = src.to_scalar();
233233
let addr = self.cast_from_int_like(scalar, src.layout, self.tcx.types.usize)?;
234-
let addr = addr.to_machine_usize(self)?;
234+
let addr = addr.to_target_usize(self)?;
235235

236236
// Then turn address into pointer.
237237
let ptr = M::ptr_from_addr_cast(&self, addr)?;

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
639639
}
640640

641641
ty::Slice(_) | ty::Str => {
642-
let len = metadata.unwrap_meta().to_machine_usize(self)?;
642+
let len = metadata.unwrap_meta().to_target_usize(self)?;
643643
let elem = layout.field(self, 0);
644644

645645
// Make sure the slice is not too big.

0 commit comments

Comments
 (0)