Skip to content

Commit b280af4

Browse files
committed
Auto merge of #3713 - rust-lang:rustup-2024-06-27, r=RalfJung
Automatic Rustup
2 parents fc94ce2 + 4cc16a5 commit b280af4

File tree

311 files changed

+5874
-2695
lines changed

Some content is hidden

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

311 files changed

+5874
-2695
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: LLVM

.reuse/dep5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Files: compiler/*
2929
x
3030
x.ps1
3131
x.py
32+
.clang-format
3233
.editorconfig
3334
.git-blame-ignore-revs
3435
.gitattributes

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,6 +5627,7 @@ dependencies = [
56275627
"regex",
56285628
"rustc-hash",
56295629
"semver",
5630+
"similar",
56305631
"termcolor",
56315632
"walkdir",
56325633
]

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,8 @@ pub struct BareFnTy {
21262126
pub ext: Extern,
21272127
pub generic_params: ThinVec<GenericParam>,
21282128
pub decl: P<FnDecl>,
2129-
/// Span of the `fn(...) -> ...` part.
2129+
/// Span of the `[unsafe] [extern] fn(...) -> ...` part, i.e. everything
2130+
/// after the generic params (if there are any, e.g. `for<'a>`).
21302131
pub decl_span: Span,
21312132
}
21322133

compiler/rustc_ast/src/util/parser.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ pub const PREC_JUMP: i8 = -30;
233233
pub const PREC_RANGE: i8 = -10;
234234
// The range 2..=14 is reserved for AssocOp binary operator precedences.
235235
pub const PREC_PREFIX: i8 = 50;
236-
pub const PREC_POSTFIX: i8 = 60;
237-
pub const PREC_PAREN: i8 = 99;
236+
pub const PREC_UNAMBIGUOUS: i8 = 60;
238237
pub const PREC_FORCE_PAREN: i8 = 100;
239238

240239
#[derive(Debug, Clone, Copy)]
@@ -325,37 +324,35 @@ impl ExprPrecedence {
325324
| ExprPrecedence::Let
326325
| ExprPrecedence::Unary => PREC_PREFIX,
327326

328-
// Unary, postfix
329-
ExprPrecedence::Await
327+
// Never need parens
328+
ExprPrecedence::Array
329+
| ExprPrecedence::Await
330+
| ExprPrecedence::Block
330331
| ExprPrecedence::Call
331-
| ExprPrecedence::MethodCall
332+
| ExprPrecedence::ConstBlock
332333
| ExprPrecedence::Field
334+
| ExprPrecedence::ForLoop
335+
| ExprPrecedence::FormatArgs
336+
| ExprPrecedence::Gen
337+
| ExprPrecedence::If
333338
| ExprPrecedence::Index
334-
| ExprPrecedence::Try
335339
| ExprPrecedence::InlineAsm
340+
| ExprPrecedence::Lit
341+
| ExprPrecedence::Loop
336342
| ExprPrecedence::Mac
337-
| ExprPrecedence::FormatArgs
343+
| ExprPrecedence::Match
344+
| ExprPrecedence::MethodCall
338345
| ExprPrecedence::OffsetOf
339-
| ExprPrecedence::PostfixMatch => PREC_POSTFIX,
340-
341-
// Never need parens
342-
ExprPrecedence::Array
346+
| ExprPrecedence::Paren
347+
| ExprPrecedence::Path
348+
| ExprPrecedence::PostfixMatch
343349
| ExprPrecedence::Repeat
350+
| ExprPrecedence::Struct
351+
| ExprPrecedence::Try
352+
| ExprPrecedence::TryBlock
344353
| ExprPrecedence::Tup
345-
| ExprPrecedence::Lit
346-
| ExprPrecedence::Path
347-
| ExprPrecedence::Paren
348-
| ExprPrecedence::If
349354
| ExprPrecedence::While
350-
| ExprPrecedence::ForLoop
351-
| ExprPrecedence::Loop
352-
| ExprPrecedence::Match
353-
| ExprPrecedence::ConstBlock
354-
| ExprPrecedence::Block
355-
| ExprPrecedence::TryBlock
356-
| ExprPrecedence::Gen
357-
| ExprPrecedence::Struct
358-
| ExprPrecedence::Err => PREC_PAREN,
355+
| ExprPrecedence::Err => PREC_UNAMBIGUOUS,
359356
}
360357
}
361358
}

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ ast_lowering_never_pattern_with_guard =
130130
131131
ast_lowering_no_precise_captures_on_apit = `use<...>` precise capturing syntax not allowed in argument-position `impl Trait`
132132
133+
ast_lowering_no_precise_captures_on_rpitit = `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
134+
.note = currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
135+
133136
ast_lowering_previously_used_here = previously used here
134137
135138
ast_lowering_register1 = register `{$reg1_name}`

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
6666
let Ok(sig_id) = sig_id else {
6767
return false;
6868
};
69-
if let Some(local_sig_id) = sig_id.as_local() {
69+
self.has_self(sig_id, span)
70+
}
71+
72+
fn has_self(&self, def_id: DefId, span: Span) -> bool {
73+
if let Some(local_sig_id) = def_id.as_local() {
7074
// The value may be missing due to recursive delegation.
7175
// Error will be emmited later during HIR ty lowering.
7276
self.resolver.delegation_fn_sigs.get(&local_sig_id).map_or(false, |sig| sig.has_self)
7377
} else {
74-
match self.tcx.def_kind(sig_id) {
78+
match self.tcx.def_kind(def_id) {
7579
DefKind::Fn => false,
76-
DefKind::AssocFn => self.tcx.associated_item(sig_id).fn_has_self_parameter,
80+
DefKind::AssocFn => self.tcx.associated_item(def_id).fn_has_self_parameter,
7781
_ => span_bug!(span, "unexpected DefKind for delegation item"),
7882
}
7983
}
@@ -107,12 +111,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
107111
span: Span,
108112
) -> Result<DefId, ErrorGuaranteed> {
109113
let sig_id = if self.is_in_trait_impl { item_id } else { path_id };
110-
let sig_id =
111-
self.resolver.get_partial_res(sig_id).and_then(|r| r.expect_full_res().opt_def_id());
112-
sig_id.ok_or_else(|| {
113-
self.tcx
114-
.dcx()
115-
.span_delayed_bug(span, "LoweringContext: couldn't resolve delegation item")
114+
self.get_resolution_id(sig_id, span)
115+
}
116+
117+
fn get_resolution_id(&self, node_id: NodeId, span: Span) -> Result<DefId, ErrorGuaranteed> {
118+
let def_id =
119+
self.resolver.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id());
120+
def_id.ok_or_else(|| {
121+
self.tcx.dcx().span_delayed_bug(
122+
span,
123+
format!("LoweringContext: couldn't resolve node {:?} in delegation item", node_id),
124+
)
116125
})
117126
}
118127

@@ -122,7 +131,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
122131
predicates: &[],
123132
has_where_clause_predicates: false,
124133
where_clause_span: span,
125-
span: span,
134+
span,
126135
})
127136
}
128137

@@ -222,12 +231,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
222231
}));
223232

224233
let path = self.arena.alloc(hir::Path { span, res: Res::Local(param_id), segments });
225-
226-
hir::Expr {
227-
hir_id: self.next_id(),
228-
kind: hir::ExprKind::Path(hir::QPath::Resolved(None, path)),
229-
span,
230-
}
234+
self.mk_expr(hir::ExprKind::Path(hir::QPath::Resolved(None, path)), span)
231235
}
232236

233237
fn lower_delegation_body(
@@ -236,19 +240,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
236240
param_count: usize,
237241
span: Span,
238242
) -> BodyId {
239-
let path = self.lower_qpath(
240-
delegation.id,
241-
&delegation.qself,
242-
&delegation.path,
243-
ParamMode::Optional,
244-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
245-
None,
246-
);
247243
let block = delegation.body.as_deref();
248244

249245
self.lower_body(|this| {
250-
let mut parameters: Vec<hir::Param<'_>> = Vec::new();
251-
let mut args: Vec<hir::Expr<'hir>> = Vec::new();
246+
let mut parameters: Vec<hir::Param<'_>> = Vec::with_capacity(param_count);
247+
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
252248

253249
for idx in 0..param_count {
254250
let (param, pat_node_id) = this.generate_param(span);
@@ -264,55 +260,49 @@ impl<'hir> LoweringContext<'_, 'hir> {
264260
};
265261
self_resolver.visit_block(block);
266262
let block = this.lower_block(block, false);
267-
hir::Expr {
268-
hir_id: this.next_id(),
269-
kind: hir::ExprKind::Block(block, None),
270-
span: block.span,
271-
}
263+
this.mk_expr(hir::ExprKind::Block(block, None), block.span)
272264
} else {
273265
let pat_hir_id = this.lower_node_id(pat_node_id);
274266
this.generate_arg(pat_hir_id, span)
275267
};
276268
args.push(arg);
277269
}
278270

279-
let args = self.arena.alloc_from_iter(args);
280-
let final_expr = this.generate_call(path, args);
271+
let final_expr = this.finalize_body_lowering(delegation, args, span);
281272
(this.arena.alloc_from_iter(parameters), final_expr)
282273
})
283274
}
284275

285-
fn generate_call(
276+
// Generates fully qualified call for the resulting body.
277+
fn finalize_body_lowering(
286278
&mut self,
287-
path: hir::QPath<'hir>,
288-
args: &'hir [hir::Expr<'hir>],
279+
delegation: &Delegation,
280+
args: Vec<hir::Expr<'hir>>,
281+
span: Span,
289282
) -> hir::Expr<'hir> {
290-
let callee = self.arena.alloc(hir::Expr {
291-
hir_id: self.next_id(),
292-
kind: hir::ExprKind::Path(path),
293-
span: path.span(),
294-
});
283+
let path = self.lower_qpath(
284+
delegation.id,
285+
&delegation.qself,
286+
&delegation.path,
287+
ParamMode::Optional,
288+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
289+
None,
290+
);
295291

296-
let expr = self.arena.alloc(hir::Expr {
297-
hir_id: self.next_id(),
298-
kind: hir::ExprKind::Call(callee, args),
299-
span: path.span(),
300-
});
292+
let args = self.arena.alloc_from_iter(args);
293+
let path_expr = self.arena.alloc(self.mk_expr(hir::ExprKind::Path(path), span));
294+
let call = self.arena.alloc(self.mk_expr(hir::ExprKind::Call(path_expr, args), span));
301295

302296
let block = self.arena.alloc(hir::Block {
303297
stmts: &[],
304-
expr: Some(expr),
298+
expr: Some(call),
305299
hir_id: self.next_id(),
306300
rules: hir::BlockCheckMode::DefaultBlock,
307-
span: path.span(),
301+
span,
308302
targeted_by_break: false,
309303
});
310304

311-
hir::Expr {
312-
hir_id: self.next_id(),
313-
kind: hir::ExprKind::Block(block, None),
314-
span: path.span(),
315-
}
305+
self.mk_expr(hir::ExprKind::Block(block, None), span)
316306
}
317307

318308
fn generate_delegation_error(
@@ -333,11 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
333323
let header = self.generate_header_error();
334324
let sig = hir::FnSig { decl, header, span };
335325

336-
let body_id = self.lower_body(|this| {
337-
let expr =
338-
hir::Expr { hir_id: this.next_id(), kind: hir::ExprKind::Err(err), span: span };
339-
(&[], expr)
340-
});
326+
let body_id = self.lower_body(|this| (&[], this.mk_expr(hir::ExprKind::Err(err), span)));
341327
DelegationResults { generics, body_id, sig }
342328
}
343329

@@ -349,6 +335,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
349335
abi: abi::Abi::Rust,
350336
}
351337
}
338+
339+
#[inline]
340+
fn mk_expr(&mut self, kind: hir::ExprKind<'hir>, span: Span) -> hir::Expr<'hir> {
341+
hir::Expr { hir_id: self.next_id(), kind, span }
342+
}
352343
}
353344

354345
struct SelfResolver<'a> {

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ pub(crate) struct NoPreciseCapturesOnApit {
424424
pub span: Span,
425425
}
426426

427+
#[derive(Diagnostic)]
428+
#[diag(ast_lowering_no_precise_captures_on_rpitit)]
429+
#[note]
430+
pub(crate) struct NoPreciseCapturesOnRpitit {
431+
#[primary_span]
432+
pub span: Span,
433+
}
434+
427435
#[derive(Diagnostic)]
428436
#[diag(ast_lowering_yield_in_closure)]
429437
pub(crate) struct YieldInClosure {

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15941594
};
15951595
debug!(?captured_lifetimes_to_duplicate);
15961596

1597+
match fn_kind {
1598+
// Deny `use<>` on RPITIT in trait/trait-impl for now.
1599+
Some(FnDeclKind::Trait | FnDeclKind::Impl) => {
1600+
if let Some(span) = bounds.iter().find_map(|bound| match *bound {
1601+
ast::GenericBound::Use(_, span) => Some(span),
1602+
_ => None,
1603+
}) {
1604+
self.tcx.dcx().emit_err(errors::NoPreciseCapturesOnRpitit { span });
1605+
}
1606+
}
1607+
None
1608+
| Some(
1609+
FnDeclKind::Fn
1610+
| FnDeclKind::Inherent
1611+
| FnDeclKind::ExternFn
1612+
| FnDeclKind::Closure
1613+
| FnDeclKind::Pointer,
1614+
) => {}
1615+
}
1616+
15971617
self.lower_opaque_inner(
15981618
opaque_ty_node_id,
15991619
origin,

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl<'a> AstValidator<'a> {
464464
{
465465
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
466466
item_span: span,
467-
block: self.current_extern_span(),
467+
block: self.current_extern_span().shrink_to_lo(),
468468
});
469469
}
470470
}

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub enum ExternBlockSuggestion {
221221
pub struct InvalidSafetyOnExtern {
222222
#[primary_span]
223223
pub item_span: Span,
224-
#[suggestion(code = "", applicability = "maybe-incorrect")]
224+
#[suggestion(code = "unsafe ", applicability = "machine-applicable", style = "verbose")]
225225
pub block: Span,
226226
}
227227

0 commit comments

Comments
 (0)