Skip to content

Commit e999c48

Browse files
authored
Merge pull request #4095 from rust-lang/rustup-2024-12-15
Automatic Rustup
2 parents d5322f7 + 581989d commit e999c48

File tree

669 files changed

+7062
-4609
lines changed

Some content is hidden

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

669 files changed

+7062
-4609
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,6 @@ dependencies = [
35073507
"cc",
35083508
"either",
35093509
"itertools",
3510-
"jobserver",
35113510
"libc",
35123511
"object 0.36.5",
35133512
"pathdiff",

compiler/rustc_ast/src/ast.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ impl Expr {
13821382
| ExprKind::Tup(_)
13831383
| ExprKind::Type(..)
13841384
| ExprKind::Underscore
1385+
| ExprKind::UnsafeBinderCast(..)
13851386
| ExprKind::While(..)
13861387
| ExprKind::Err(_)
13871388
| ExprKind::Dummy => ExprPrecedence::Unambiguous,
@@ -1509,7 +1510,13 @@ pub enum ExprKind {
15091510
/// `'label: for await? pat in iter { block }`
15101511
///
15111512
/// This is desugared to a combination of `loop` and `match` expressions.
1512-
ForLoop { pat: P<Pat>, iter: P<Expr>, body: P<Block>, label: Option<Label>, kind: ForLoopKind },
1513+
ForLoop {
1514+
pat: P<Pat>,
1515+
iter: P<Expr>,
1516+
body: P<Block>,
1517+
label: Option<Label>,
1518+
kind: ForLoopKind,
1519+
},
15131520
/// Conditionless loop (can be exited with `break`, `continue`, or `return`).
15141521
///
15151522
/// `'label: loop { block }`
@@ -1614,6 +1621,8 @@ pub enum ExprKind {
16141621
/// A `format_args!()` expression.
16151622
FormatArgs(P<FormatArgs>),
16161623

1624+
UnsafeBinderCast(UnsafeBinderCastKind, P<Expr>, Option<P<Ty>>),
1625+
16171626
/// Placeholder for an expression that wasn't syntactically well formed in some way.
16181627
Err(ErrorGuaranteed),
16191628

@@ -1652,6 +1661,16 @@ impl GenBlockKind {
16521661
}
16531662
}
16541663

1664+
/// Whether we're unwrapping or wrapping an unsafe binder
1665+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1666+
#[derive(Encodable, Decodable, HashStable_Generic)]
1667+
pub enum UnsafeBinderCastKind {
1668+
// e.g. `&i32` -> `unsafe<'a> &'a i32`
1669+
Wrap,
1670+
// e.g. `unsafe<'a> &'a i32` -> `&i32`
1671+
Unwrap,
1672+
}
1673+
16551674
/// The explicit `Self` type in a "qualified path". The actual
16561675
/// path, including the trait and the associated item, is stored
16571676
/// separately. `position` represents the index of the associated
@@ -2223,6 +2242,12 @@ pub struct BareFnTy {
22232242
pub decl_span: Span,
22242243
}
22252244

2245+
#[derive(Clone, Encodable, Decodable, Debug)]
2246+
pub struct UnsafeBinderTy {
2247+
pub generic_params: ThinVec<GenericParam>,
2248+
pub inner_ty: P<Ty>,
2249+
}
2250+
22262251
/// The various kinds of type recognized by the compiler.
22272252
//
22282253
// Adding a new variant? Please update `test_ty` in `tests/ui/macros/stringify.rs`.
@@ -2242,6 +2267,8 @@ pub enum TyKind {
22422267
PinnedRef(Option<Lifetime>, MutTy),
22432268
/// A bare function (e.g., `fn(usize) -> bool`).
22442269
BareFn(P<BareFnTy>),
2270+
/// An unsafe existential lifetime binder (e.g., `unsafe<'a> &'a ()`).
2271+
UnsafeBinder(P<UnsafeBinderTy>),
22452272
/// The never type (`!`).
22462273
Never,
22472274
/// A tuple (`(A, B, C, D,...)`).
@@ -2877,7 +2904,7 @@ pub enum ModKind {
28772904
/// or with definition outlined to a separate file `mod foo;` and already loaded from it.
28782905
/// The inner span is from the first token past `{` to the last token until `}`,
28792906
/// or from the first to the last token in the loaded file.
2880-
Loaded(ThinVec<P<Item>>, Inline, ModSpans),
2907+
Loaded(ThinVec<P<Item>>, Inline, ModSpans, Result<(), ErrorGuaranteed>),
28812908
/// Module with definition outlined to a separate file `mod foo;` but not yet loaded from it.
28822909
Unloaded,
28832910
}

compiler/rustc_ast/src/mut_visit.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
558558
vis.visit_fn_decl(decl);
559559
vis.visit_span(decl_span);
560560
}
561+
TyKind::UnsafeBinder(binder) => {
562+
let UnsafeBinderTy { generic_params, inner_ty } = binder.deref_mut();
563+
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
564+
vis.visit_ty(inner_ty);
565+
}
561566
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
562567
TyKind::Paren(ty) => vis.visit_ty(ty),
563568
TyKind::Pat(ty, pat) => {
@@ -1212,7 +1217,12 @@ impl WalkItemKind for ItemKind {
12121217
ItemKind::Mod(safety, mod_kind) => {
12131218
visit_safety(vis, safety);
12141219
match mod_kind {
1215-
ModKind::Loaded(items, _inline, ModSpans { inner_span, inject_use_span }) => {
1220+
ModKind::Loaded(
1221+
items,
1222+
_inline,
1223+
ModSpans { inner_span, inject_use_span },
1224+
_,
1225+
) => {
12161226
items.flat_map_in_place(|item| vis.flat_map_item(item));
12171227
vis.visit_span(inner_span);
12181228
vis.visit_span(inject_use_span);
@@ -1775,6 +1785,12 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
17751785
ExprKind::TryBlock(body) => vis.visit_block(body),
17761786
ExprKind::Lit(_token) => {}
17771787
ExprKind::IncludedBytes(_bytes) => {}
1788+
ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
1789+
vis.visit_expr(expr);
1790+
if let Some(ty) = ty {
1791+
vis.visit_ty(ty);
1792+
}
1793+
}
17781794
ExprKind::Err(_guar) => {}
17791795
ExprKind::Dummy => {}
17801796
}

compiler/rustc_ast/src/util/classify.rs

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
152152
| Underscore
153153
| Yeet(..)
154154
| Yield(..)
155+
| UnsafeBinderCast(..)
155156
| Err(..)
156157
| Dummy => return false,
157158
}
@@ -232,6 +233,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
232233
| Paren(_)
233234
| Try(_)
234235
| Yeet(None)
236+
| UnsafeBinderCast(..)
235237
| Err(_)
236238
| Dummy => break None,
237239
}
@@ -253,6 +255,10 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
253255
ty = &mut_ty.ty;
254256
}
255257

258+
ast::TyKind::UnsafeBinder(binder) => {
259+
ty = &binder.inner_ty;
260+
}
261+
256262
ast::TyKind::BareFn(fn_ty) => match &fn_ty.decl.output {
257263
ast::FnRetTy::Default(_) => break None,
258264
ast::FnRetTy::Ty(ret) => ty = ret,

compiler/rustc_ast/src/visit.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl WalkItemKind for ItemKind {
380380
try_visit!(visitor.visit_fn(kind, span, id));
381381
}
382382
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
383-
ModKind::Loaded(items, _inline, _inner_span) => {
383+
ModKind::Loaded(items, _inline, _inner_span, _) => {
384384
walk_list!(visitor, visit_item, items);
385385
}
386386
ModKind::Unloaded => {}
@@ -522,6 +522,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
522522
walk_list!(visitor, visit_generic_param, generic_params);
523523
try_visit!(visitor.visit_fn_decl(decl));
524524
}
525+
TyKind::UnsafeBinder(binder) => {
526+
walk_list!(visitor, visit_generic_param, &binder.generic_params);
527+
try_visit!(visitor.visit_ty(&binder.inner_ty));
528+
}
525529
TyKind::Path(maybe_qself, path) => {
526530
try_visit!(visitor.visit_qself(maybe_qself));
527531
try_visit!(visitor.visit_path(path, *id));
@@ -1226,6 +1230,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
12261230
ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),
12271231
ExprKind::Lit(_token) => {}
12281232
ExprKind::IncludedBytes(_bytes) => {}
1233+
ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
1234+
try_visit!(visitor.visit_expr(expr));
1235+
visit_opt!(visitor, visit_ty, ty);
1236+
}
12291237
ExprKind::Err(_guar) => {}
12301238
ExprKind::Dummy => {}
12311239
}

compiler/rustc_ast_lowering/src/block.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
22
use rustc_hir as hir;
3+
use rustc_span::sym;
34
use smallvec::SmallVec;
45

56
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
@@ -82,11 +83,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
8283
(self.arena.alloc_from_iter(stmts), expr)
8384
}
8485

86+
/// Return an `ImplTraitContext` that allows impl trait in bindings if
87+
/// the feature gate is enabled, or issues a feature error if it is not.
88+
fn impl_trait_in_bindings_ctxt(&self, position: ImplTraitPosition) -> ImplTraitContext {
89+
if self.tcx.features().impl_trait_in_bindings() {
90+
ImplTraitContext::InBinding
91+
} else {
92+
ImplTraitContext::FeatureGated(position, sym::impl_trait_in_bindings)
93+
}
94+
}
95+
8596
fn lower_local(&mut self, l: &Local) -> &'hir hir::LetStmt<'hir> {
86-
let ty = l
87-
.ty
88-
.as_ref()
89-
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
97+
// Let statements are allowed to have impl trait in bindings.
98+
let ty = l.ty.as_ref().map(|t| {
99+
self.lower_ty(t, self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable))
100+
});
90101
let init = l.kind.init().map(|init| self.lower_expr(init));
91102
let hir_id = self.lower_node_id(l.id);
92103
let pat = self.lower_pat(&l.pat);

compiler/rustc_ast_lowering/src/expr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
379379
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
380380
ExprKind::Err(guar) => hir::ExprKind::Err(*guar),
381381

382+
ExprKind::UnsafeBinderCast(kind, expr, ty) => hir::ExprKind::UnsafeBinderCast(
383+
*kind,
384+
self.lower_expr(expr),
385+
ty.as_ref().map(|ty| {
386+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast))
387+
}),
388+
),
389+
382390
ExprKind::Dummy => {
383391
span_bug!(e.span, "lowered ExprKind::Dummy")
384392
}

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
238238
})
239239
}
240240
ItemKind::Mod(_, mod_kind) => match mod_kind {
241-
ModKind::Loaded(items, _, spans) => {
241+
ModKind::Loaded(items, _, spans, _) => {
242242
hir::ItemKind::Mod(self.lower_mod(items, spans))
243243
}
244244
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),

compiler/rustc_ast_lowering/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ enum ImplTraitContext {
260260
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
261261
///
262262
OpaqueTy { origin: hir::OpaqueTyOrigin<LocalDefId> },
263+
264+
/// Treat `impl Trait` as a "trait ascription", which is like a type
265+
/// variable but that also enforces that a set of trait goals hold.
266+
///
267+
/// This is useful to guide inference for unnameable types.
268+
InBinding,
269+
263270
/// `impl Trait` is unstably accepted in this position.
264271
FeatureGated(ImplTraitPosition, Symbol),
265272
/// `impl Trait` is not accepted in this position.
@@ -1228,6 +1235,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12281235
param_names: self.lower_fn_params_to_names(&f.decl),
12291236
}))
12301237
}
1238+
TyKind::UnsafeBinder(f) => {
1239+
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
1240+
hir::TyKind::UnsafeBinder(self.arena.alloc(hir::UnsafeBinderTy {
1241+
generic_params,
1242+
inner_ty: self.lower_ty(&f.inner_ty, itctx),
1243+
}))
1244+
}
12311245
TyKind::Never => hir::TyKind::Never,
12321246
TyKind::Tup(tys) => hir::TyKind::Tup(
12331247
self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty_direct(ty, itctx))),
@@ -1320,6 +1334,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13201334
}
13211335
path
13221336
}
1337+
ImplTraitContext::InBinding => {
1338+
hir::TyKind::TraitAscription(self.lower_param_bounds(bounds, itctx))
1339+
}
13231340
ImplTraitContext::FeatureGated(position, feature) => {
13241341
let guar = self
13251342
.tcx

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10291029
self.dcx().emit_err(errors::UnsafeItem { span, kind: "module" });
10301030
}
10311031
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
1032-
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _))
1032+
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _, _))
10331033
&& !attr::contains_name(&item.attrs, sym::path)
10341034
{
10351035
self.check_mod_file_item_asciionly(item.ident);

compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
560560
gate_all!(return_type_notation, "return type notation is experimental");
561561
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
562562
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
563+
gate_all!(unsafe_binders, "unsafe binder types are experimental");
563564

564565
if !visitor.features.never_patterns() {
565566
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_ast_pretty/src/pprust/state.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,14 @@ impl<'a> State<'a> {
11981198
ast::TyKind::BareFn(f) => {
11991199
self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
12001200
}
1201+
ast::TyKind::UnsafeBinder(f) => {
1202+
self.ibox(INDENT_UNIT);
1203+
self.word("unsafe");
1204+
self.print_generic_params(&f.generic_params);
1205+
self.nbsp();
1206+
self.print_type(&f.inner_ty);
1207+
self.end();
1208+
}
12011209
ast::TyKind::Path(None, path) => {
12021210
self.print_path(path, false, 0);
12031211
}

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+19
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,25 @@ impl<'a> State<'a> {
772772
self.word_nbsp("try");
773773
self.print_block_with_attrs(blk, attrs)
774774
}
775+
ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => {
776+
self.word("builtin # ");
777+
match kind {
778+
ast::UnsafeBinderCastKind::Wrap => self.word("wrap_binder"),
779+
ast::UnsafeBinderCastKind::Unwrap => self.word("unwrap_binder"),
780+
}
781+
self.popen();
782+
self.ibox(0);
783+
self.print_expr(expr, FixupContext::default());
784+
785+
if let Some(ty) = ty {
786+
self.word(",");
787+
self.space();
788+
self.print_type(ty);
789+
}
790+
791+
self.end();
792+
self.pclose();
793+
}
775794
ast::ExprKind::Err(_) => {
776795
self.popen();
777796
self.word("/*ERROR*/");

0 commit comments

Comments
 (0)