Skip to content

Commit f520900

Browse files
committed
Auto merge of #149853 - matthiaskrgr:rollup-m2rkwqr, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #148052 (Stabilize `const_mul_add`) - #149386 (Display funding link in the github overview) - #149489 (Experimentally add *heterogeneous* `try` blocks) - #149764 (Make `--print=backend-has-zstd` work by default on any backend) - #149838 (Build auxiliary in pretty tests) - #149839 (Use `PointeeSized` bound for `TrivialClone` impls) - #149846 (Statically require links to an issue or the edition guide for all FCWs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a9ac706 + eab1036 commit f520900

File tree

87 files changed

+704
-386
lines changed

Some content is hidden

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

87 files changed

+704
-386
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: ["rust-lang.org/funding"]

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,8 +1819,14 @@ pub enum ExprKind {
18191819
/// A use expression (`x.use`). Span is of use keyword.
18201820
Use(Box<Expr>, Span),
18211821

1822-
/// A try block (`try { ... }`).
1823-
TryBlock(Box<Block>),
1822+
/// A try block (`try { ... }`), if the type is `None`, or
1823+
/// A try block (`try bikeshed Ty { ... }`) if the type is `Some`.
1824+
///
1825+
/// Note that `try bikeshed` is a *deliberately ridiculous* placeholder
1826+
/// syntax to avoid deciding what keyword or symbol should go there.
1827+
/// It's that way for experimentation only; an RFC to decide the final
1828+
/// semantics and syntax would be needed to put it on stabilization-track.
1829+
TryBlock(Box<Block>, Option<Box<Ty>>),
18241830

18251831
/// An assignment (`a = foo()`).
18261832
/// The `Span` argument is the span of the `=` token.

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,8 @@ macro_rules! common_visitor_and_walkers {
10481048
visit_visitable!($($mut)? vis, kind),
10491049
ExprKind::Try(subexpression) =>
10501050
visit_visitable!($($mut)? vis, subexpression),
1051-
ExprKind::TryBlock(body) =>
1052-
visit_visitable!($($mut)? vis, body),
1051+
ExprKind::TryBlock(body, optional_type) =>
1052+
visit_visitable!($($mut)? vis, body, optional_type),
10531053
ExprKind::Lit(token) =>
10541054
visit_visitable!($($mut)? vis, token),
10551055
ExprKind::IncludedBytes(bytes) =>

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::mem;
12
use std::ops::ControlFlow;
23
use std::sync::Arc;
34

@@ -27,7 +28,9 @@ use super::{
2728
GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt,
2829
};
2930
use crate::errors::{InvalidLegacyConstGenericArg, UseConstGenericArg, YieldInClosure};
30-
use crate::{AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, fluent_generated};
31+
use crate::{
32+
AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, TryBlockScope, fluent_generated,
33+
};
3134

3235
struct WillCreateDefIdsVisitor {}
3336

@@ -199,7 +202,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
199202
)
200203
})
201204
}
202-
ExprKind::TryBlock(body) => self.lower_expr_try_block(body),
205+
ExprKind::TryBlock(body, opt_ty) => {
206+
self.lower_expr_try_block(body, opt_ty.as_deref())
207+
}
203208
ExprKind::Match(expr, arms, kind) => hir::ExprKind::Match(
204209
self.lower_expr(expr),
205210
self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
@@ -562,9 +567,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
562567
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
563568
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
564569
/// and save the block id to use it as a break target for desugaring of the `?` operator.
565-
fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
570+
fn lower_expr_try_block(&mut self, body: &Block, opt_ty: Option<&Ty>) -> hir::ExprKind<'hir> {
566571
let body_hir_id = self.lower_node_id(body.id);
567-
self.with_catch_scope(body_hir_id, |this| {
572+
let new_scope = if opt_ty.is_some() {
573+
TryBlockScope::Heterogeneous(body_hir_id)
574+
} else {
575+
TryBlockScope::Homogeneous(body_hir_id)
576+
};
577+
let whole_block = self.with_try_block_scope(new_scope, |this| {
568578
let mut block = this.lower_block_noalloc(body_hir_id, body, true);
569579

570580
// Final expression of the block (if present) or `()` with span at the end of block
@@ -598,8 +608,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
598608
ok_wrapped_span,
599609
));
600610

601-
hir::ExprKind::Block(this.arena.alloc(block), None)
602-
})
611+
this.arena.alloc(block)
612+
});
613+
614+
if let Some(ty) = opt_ty {
615+
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path));
616+
let block_expr = self.arena.alloc(self.expr_block(whole_block));
617+
hir::ExprKind::Type(block_expr, ty)
618+
} else {
619+
hir::ExprKind::Block(whole_block, None)
620+
}
603621
}
604622

605623
fn wrap_in_try_constructor(
@@ -1617,10 +1635,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
16171635
}
16181636
}
16191637

1620-
fn with_catch_scope<T>(&mut self, catch_id: hir::HirId, f: impl FnOnce(&mut Self) -> T) -> T {
1621-
let old_scope = self.catch_scope.replace(catch_id);
1638+
fn with_try_block_scope<T>(
1639+
&mut self,
1640+
scope: TryBlockScope,
1641+
f: impl FnOnce(&mut Self) -> T,
1642+
) -> T {
1643+
let old_scope = mem::replace(&mut self.try_block_scope, scope);
16221644
let result = f(self);
1623-
self.catch_scope = old_scope;
1645+
self.try_block_scope = old_scope;
16241646
result
16251647
}
16261648

@@ -1978,18 +2000,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
19782000
let residual_ident = Ident::with_dummy_span(sym::residual);
19792001
let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
19802002
let residual_expr = self.expr_ident_mut(try_span, residual_ident, residual_local_nid);
2003+
2004+
let (constructor_item, target_id) = match self.try_block_scope {
2005+
TryBlockScope::Function => {
2006+
(hir::LangItem::TryTraitFromResidual, Err(hir::LoopIdError::OutsideLoopScope))
2007+
}
2008+
TryBlockScope::Homogeneous(block_id) => {
2009+
(hir::LangItem::ResidualIntoTryType, Ok(block_id))
2010+
}
2011+
TryBlockScope::Heterogeneous(block_id) => {
2012+
(hir::LangItem::TryTraitFromResidual, Ok(block_id))
2013+
}
2014+
};
19812015
let from_residual_expr = self.wrap_in_try_constructor(
1982-
if self.catch_scope.is_some() {
1983-
hir::LangItem::ResidualIntoTryType
1984-
} else {
1985-
hir::LangItem::TryTraitFromResidual
1986-
},
2016+
constructor_item,
19872017
try_span,
19882018
self.arena.alloc(residual_expr),
19892019
unstable_span,
19902020
);
1991-
let ret_expr = if let Some(catch_id) = self.catch_scope {
1992-
let target_id = Ok(catch_id);
2021+
let ret_expr = if target_id.is_ok() {
19932022
self.arena.alloc(self.expr(
19942023
try_span,
19952024
hir::ExprKind::Break(
@@ -2044,11 +2073,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
20442073
yeeted_span,
20452074
);
20462075

2047-
if let Some(catch_id) = self.catch_scope {
2048-
let target_id = Ok(catch_id);
2049-
hir::ExprKind::Break(hir::Destination { label: None, target_id }, Some(from_yeet_expr))
2050-
} else {
2051-
self.checked_return(Some(from_yeet_expr))
2076+
match self.try_block_scope {
2077+
TryBlockScope::Homogeneous(block_id) | TryBlockScope::Heterogeneous(block_id) => {
2078+
hir::ExprKind::Break(
2079+
hir::Destination { label: None, target_id: Ok(block_id) },
2080+
Some(from_yeet_expr),
2081+
)
2082+
}
2083+
TryBlockScope::Function => self.checked_return(Some(from_yeet_expr)),
20522084
}
20532085
}
20542086

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#![feature(if_let_guard)]
3636
// tidy-alphabetical-end
3737

38+
use std::mem;
3839
use std::sync::Arc;
3940

4041
use rustc_ast::node_id::NodeMap;
@@ -117,7 +118,7 @@ struct LoweringContext<'a, 'hir> {
117118
/// outside of an `async fn`.
118119
current_item: Option<Span>,
119120

120-
catch_scope: Option<HirId>,
121+
try_block_scope: TryBlockScope,
121122
loop_scope: Option<HirId>,
122123
is_in_loop_condition: bool,
123124
is_in_dyn_type: bool,
@@ -173,7 +174,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
173174
trait_map: Default::default(),
174175

175176
// Lowering state.
176-
catch_scope: None,
177+
try_block_scope: TryBlockScope::Function,
177178
loop_scope: None,
178179
is_in_loop_condition: false,
179180
is_in_dyn_type: false,
@@ -416,6 +417,18 @@ enum AstOwner<'a> {
416417
ForeignItem(&'a ast::ForeignItem),
417418
}
418419

420+
#[derive(Copy, Clone, Debug)]
421+
enum TryBlockScope {
422+
/// There isn't a `try` block, so a `?` will use `return`.
423+
Function,
424+
/// We're inside a `try { … }` block, so a `?` will block-break
425+
/// from that block using a type depending only on the argument.
426+
Homogeneous(HirId),
427+
/// We're inside a `try as _ { … }` block, so a `?` will block-break
428+
/// from that block using the type specified.
429+
Heterogeneous(HirId),
430+
}
431+
419432
fn index_crate<'a>(
420433
node_id_to_def_id: &NodeMap<LocalDefId>,
421434
krate: &'a Crate,
@@ -936,10 +949,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
936949

937950
let old_contract = self.contract_ensures.take();
938951

939-
let catch_scope = self.catch_scope.take();
952+
let try_block_scope = mem::replace(&mut self.try_block_scope, TryBlockScope::Function);
940953
let loop_scope = self.loop_scope.take();
941954
let ret = f(self);
942-
self.catch_scope = catch_scope;
955+
self.try_block_scope = try_block_scope;
943956
self.loop_scope = loop_scope;
944957

945958
self.contract_ensures = old_contract;

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,17 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
334334

335335
fn visit_expr(&mut self, e: &'a ast::Expr) {
336336
match e.kind {
337-
ast::ExprKind::TryBlock(_) => {
337+
ast::ExprKind::TryBlock(_, None) => {
338338
gate!(&self, try_blocks, e.span, "`try` expression is experimental");
339339
}
340+
ast::ExprKind::TryBlock(_, Some(_)) => {
341+
gate!(
342+
&self,
343+
try_blocks_heterogeneous,
344+
e.span,
345+
"`try bikeshed` expression is experimental"
346+
);
347+
}
340348
ast::ExprKind::Lit(token::Lit {
341349
kind: token::LitKind::Float | token::LitKind::Integer,
342350
suffix,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,10 +818,15 @@ impl<'a> State<'a> {
818818
);
819819
self.word("?")
820820
}
821-
ast::ExprKind::TryBlock(blk) => {
821+
ast::ExprKind::TryBlock(blk, opt_ty) => {
822822
let cb = self.cbox(0);
823823
let ib = self.ibox(0);
824824
self.word_nbsp("try");
825+
if let Some(ty) = opt_ty {
826+
self.word_nbsp("bikeshed");
827+
self.print_type(ty);
828+
self.space();
829+
}
825830
self.print_block_with_attrs(blk, attrs, cb, ib)
826831
}
827832
ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => {

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
315315
| ExprKind::Path(_, _)
316316
| ExprKind::Ret(_)
317317
| ExprKind::Try(_)
318-
| ExprKind::TryBlock(_)
318+
| ExprKind::TryBlock(_, _)
319319
| ExprKind::Type(_, _)
320320
| ExprKind::Underscore
321321
| ExprKind::While(_, _, _)

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_codegen_ssa::{CodegenResults, TargetConfig};
4747
use rustc_log::tracing::info;
4848
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
4949
use rustc_session::Session;
50-
use rustc_session::config::{OutputFilenames, PrintKind, PrintRequest};
50+
use rustc_session::config::OutputFilenames;
5151
use rustc_span::{Symbol, sym};
5252
use rustc_target::spec::{Abi, Arch, Env, Os};
5353

@@ -160,16 +160,6 @@ impl CodegenBackend for CraneliftCodegenBackend {
160160
}
161161
}
162162

163-
fn print(&self, req: &PrintRequest, out: &mut String, _sess: &Session) {
164-
match req.kind {
165-
// FIXME have a default impl that returns false
166-
PrintKind::BackendHasZstd => {
167-
out.push_str("false\n");
168-
}
169-
_ => {}
170-
}
171-
}
172-
173163
fn target_config(&self, sess: &Session) -> TargetConfig {
174164
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
175165
let target_features = match sess.target.arch {

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,6 @@ impl CodegenBackend for LlvmCodegenBackend {
257257
}
258258
writeln!(out).unwrap();
259259
}
260-
PrintKind::BackendHasZstd => {
261-
let has_zstd = llvm::LLVMRustLLVMHasZstdCompression();
262-
writeln!(out, "{has_zstd}").unwrap();
263-
}
264260
PrintKind::CodeModels => {
265261
writeln!(out, "Available code models:").unwrap();
266262
for name in &["tiny", "small", "kernel", "medium", "large"] {
@@ -314,6 +310,10 @@ impl CodegenBackend for LlvmCodegenBackend {
314310
llvm_util::print_version();
315311
}
316312

313+
fn has_zstd(&self) -> bool {
314+
llvm::LLVMRustLLVMHasZstdCompression()
315+
}
316+
317317
fn target_config(&self, sess: &Session) -> TargetConfig {
318318
target_config(sess)
319319
}

0 commit comments

Comments
 (0)