Skip to content

Commit cb9ee17

Browse files
committed
Auto merge of rust-lang#126962 - workingjubilee:rollup-2qz7dbs, r=workingjubilee
Rollup of 14 pull requests Successful merges: - rust-lang#126618 (Mark assoc tys live only if the corresponding trait is live) - rust-lang#126746 (Deny `use<>` for RPITITs) - rust-lang#126868 (not use offset when there is not ends with brace) - rust-lang#126884 (Do not ICE when suggesting dereferencing closure arg) - rust-lang#126885 (Remove internal `PathBuf::as_mut_vec`) - rust-lang#126893 (Eliminate the distinction between PREC_POSTFIX and PREC_PAREN precedence level) - rust-lang#126915 (Don't suggest awaiting in closure patterns) - rust-lang#126916 (Specify target specific linker for `riscv64gc-gnu` job) - rust-lang#126926 (Tweak a confusing comment in `create_match_candidates`) - rust-lang#126927 (core: VaArgSafe is an unsafe trait) - rust-lang#126932 (Tweak `FlatPat::new` to avoid a temporarily-invalid state) - rust-lang#126941 (Migrate `run-make/llvm-ident` to `rmake.rs`) - rust-lang#126946 (Add missing slash in `const_eval_select` doc comment) - rust-lang#126947 (Delegation: ast lowering refactor) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c290e9d + c49f753 commit cb9ee17

File tree

60 files changed

+683
-277
lines changed

Some content is hidden

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

60 files changed

+683
-277
lines changed

compiler/rustc_ast/src/util/parser.rs

+21-24
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

+3
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

+49-58
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

+8
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

+20
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_pretty/src/pprust/state/expr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'a> State<'a> {
217217
fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) {
218218
let prec = match func.kind {
219219
ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
220-
_ => parser::PREC_POSTFIX,
220+
_ => parser::PREC_UNAMBIGUOUS,
221221
};
222222

223223
// Independent of parenthesization related to precedence, we must
@@ -257,7 +257,7 @@ impl<'a> State<'a> {
257257
// boundaries, `$receiver.method()` can be parsed back as a statement
258258
// containing an expression if and only if `$receiver` can be parsed as
259259
// a statement containing an expression.
260-
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX, fixup);
260+
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS, fixup);
261261

262262
self.word(".");
263263
self.print_ident(segment.ident);
@@ -489,7 +489,7 @@ impl<'a> State<'a> {
489489
self.space();
490490
}
491491
MatchKind::Postfix => {
492-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
492+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
493493
self.word_nbsp(".match");
494494
}
495495
}
@@ -549,7 +549,7 @@ impl<'a> State<'a> {
549549
self.print_block_with_attrs(blk, attrs);
550550
}
551551
ast::ExprKind::Await(expr, _) => {
552-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
552+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
553553
self.word(".await");
554554
}
555555
ast::ExprKind::Assign(lhs, rhs, _) => {
@@ -568,14 +568,14 @@ impl<'a> State<'a> {
568568
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression());
569569
}
570570
ast::ExprKind::Field(expr, ident) => {
571-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
571+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
572572
self.word(".");
573573
self.print_ident(*ident);
574574
}
575575
ast::ExprKind::Index(expr, index, _) => {
576576
self.print_expr_maybe_paren(
577577
expr,
578-
parser::PREC_POSTFIX,
578+
parser::PREC_UNAMBIGUOUS,
579579
fixup.leftmost_subexpression(),
580580
);
581581
self.word("[");
@@ -713,7 +713,7 @@ impl<'a> State<'a> {
713713
}
714714
}
715715
ast::ExprKind::Try(e) => {
716-
self.print_expr_maybe_paren(e, parser::PREC_POSTFIX, fixup);
716+
self.print_expr_maybe_paren(e, parser::PREC_UNAMBIGUOUS, fixup);
717717
self.word("?")
718718
}
719719
ast::ExprKind::TryBlock(blk) => {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,9 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
11511151
// Get the arguments for the found method, only specifying that `Self` is the receiver type.
11521152
let Some(possible_rcvr_ty) = typeck_results.node_type_opt(rcvr.hir_id) else { return };
11531153
let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
1154-
if param.index == 0 {
1154+
if let ty::GenericParamDefKind::Lifetime = param.kind {
1155+
tcx.lifetimes.re_erased.into()
1156+
} else if param.index == 0 && param.name == kw::SelfUpper {
11551157
possible_rcvr_ty.into()
11561158
} else if param.index == closure_param.index {
11571159
closure_ty.into()
@@ -1168,7 +1170,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
11681170
Obligation::misc(tcx, span, self.mir_def_id(), self.param_env, pred)
11691171
}));
11701172

1171-
if ocx.select_all_or_error().is_empty() {
1173+
if ocx.select_all_or_error().is_empty() && count > 0 {
11721174
diag.span_suggestion_verbose(
11731175
tcx.hir().body(*body).value.peel_blocks().span.shrink_to_lo(),
11741176
"dereference the return value",

0 commit comments

Comments
 (0)