Skip to content

Commit c6d42d7

Browse files
committed
Auto merge of rust-lang#148241 - matthiaskrgr:rollup-4lsapc2, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#144444 (Contract variable declarations) - rust-lang#147281 (Make diagnostics clearer for binop-related errors in foreign crates) - rust-lang#148131 (Skip parameter attribute deduction for MIR with `spread_arg`) - rust-lang#148224 (bootstrap: `ensure(doc::Std)` no longer opens a browser) - rust-lang#148226 (Bootstrap update) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4146079 + dfaaa1b commit c6d42d7

File tree

56 files changed

+931
-683
lines changed

Some content is hidden

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

56 files changed

+931
-683
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,9 @@ pub struct TraitImplHeader {
36803680

36813681
#[derive(Clone, Encodable, Decodable, Debug, Default, Walkable)]
36823682
pub struct FnContract {
3683+
/// Declarations of variables accessible both in the `requires` and
3684+
/// `ensures` clauses.
3685+
pub declarations: ThinVec<Stmt>,
36833686
pub requires: Option<Box<Expr>>,
36843687
pub ensures: Option<Box<Expr>>,
36853688
}

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2727
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
2828
}
2929

30-
fn lower_stmts(
30+
pub(super) fn lower_stmts(
3131
&mut self,
3232
mut ast_stmts: &[Stmt],
3333
) -> (&'hir [hir::Stmt<'hir>], Option<&'hir hir::Expr<'hir>>) {

compiler/rustc_ast_lowering/src/contract.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1818
body: impl FnOnce(&mut Self) -> rustc_hir::Expr<'hir>,
1919
contract: &rustc_ast::FnContract,
2020
) -> rustc_hir::Expr<'hir> {
21+
// The order in which things are lowered is important! I.e to
22+
// refer to variables in contract_decls from postcond/precond,
23+
// we must lower it first!
24+
let contract_decls = self.lower_stmts(&contract.declarations).0;
25+
2126
match (&contract.requires, &contract.ensures) {
2227
(Some(req), Some(ens)) => {
2328
// Lower the fn contract, which turns:
@@ -27,6 +32,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2732
// into:
2833
//
2934
// let __postcond = if contract_checks {
35+
// CONTRACT_DECLARATIONS;
3036
// contract_check_requires(PRECOND);
3137
// Some(|ret_val| POSTCOND)
3238
// } else {
@@ -45,8 +51,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4551
let precond = self.lower_precond(req);
4652
let postcond_checker = self.lower_postcond_checker(ens);
4753

48-
let contract_check =
49-
self.lower_contract_check_with_postcond(Some(precond), postcond_checker);
54+
let contract_check = self.lower_contract_check_with_postcond(
55+
contract_decls,
56+
Some(precond),
57+
postcond_checker,
58+
);
5059

5160
let wrapped_body =
5261
self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span);
@@ -68,15 +77,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6877
// let ret = { body };
6978
//
7079
// if contract_checks {
80+
// CONTRACT_DECLARATIONS;
7181
// contract_check_ensures(__postcond, ret)
7282
// } else {
7383
// ret
7484
// }
7585
// }
76-
7786
let postcond_checker = self.lower_postcond_checker(ens);
7887
let contract_check =
79-
self.lower_contract_check_with_postcond(None, postcond_checker);
88+
self.lower_contract_check_with_postcond(contract_decls, None, postcond_checker);
8089

8190
let wrapped_body =
8291
self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span);
@@ -91,12 +100,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
91100
//
92101
// {
93102
// if contracts_checks {
103+
// CONTRACT_DECLARATIONS;
94104
// contract_requires(PRECOND);
95105
// }
96106
// body
97107
// }
98108
let precond = self.lower_precond(req);
99-
let precond_check = self.lower_contract_check_just_precond(precond);
109+
let precond_check = self.lower_contract_check_just_precond(contract_decls, precond);
100110

101111
let body = self.arena.alloc(body(self));
102112

@@ -145,9 +155,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
145155

146156
fn lower_contract_check_just_precond(
147157
&mut self,
158+
contract_decls: &'hir [rustc_hir::Stmt<'hir>],
148159
precond: rustc_hir::Stmt<'hir>,
149160
) -> rustc_hir::Stmt<'hir> {
150-
let stmts = self.arena.alloc_from_iter([precond].into_iter());
161+
let stmts = self
162+
.arena
163+
.alloc_from_iter(contract_decls.into_iter().map(|d| *d).chain([precond].into_iter()));
151164

152165
let then_block_stmts = self.block_all(precond.span, stmts, None);
153166
let then_block = self.arena.alloc(self.expr_block(&then_block_stmts));
@@ -164,10 +177,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
164177

165178
fn lower_contract_check_with_postcond(
166179
&mut self,
180+
contract_decls: &'hir [rustc_hir::Stmt<'hir>],
167181
precond: Option<rustc_hir::Stmt<'hir>>,
168182
postcond_checker: &'hir rustc_hir::Expr<'hir>,
169183
) -> &'hir rustc_hir::Expr<'hir> {
170-
let stmts = self.arena.alloc_from_iter(precond.into_iter());
184+
let stmts = self
185+
.arena
186+
.alloc_from_iter(contract_decls.into_iter().map(|d| *d).chain(precond.into_iter()));
171187
let span = match precond {
172188
Some(precond) => precond.span,
173189
None => postcond_checker.span,

compiler/rustc_builtin_macros/src/contracts.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl AttrProcMacro for ExpandRequires {
1717
annotation: TokenStream,
1818
annotated: TokenStream,
1919
) -> Result<TokenStream, ErrorGuaranteed> {
20-
expand_requires_tts(ecx, span, annotation, annotated)
20+
expand_contract_clause_tts(ecx, span, annotation, annotated, kw::ContractRequires)
2121
}
2222
}
2323

@@ -29,7 +29,7 @@ impl AttrProcMacro for ExpandEnsures {
2929
annotation: TokenStream,
3030
annotated: TokenStream,
3131
) -> Result<TokenStream, ErrorGuaranteed> {
32-
expand_ensures_tts(ecx, span, annotation, annotated)
32+
expand_contract_clause_tts(ecx, span, annotation, annotated, kw::ContractEnsures)
3333
}
3434
}
3535

@@ -130,42 +130,17 @@ fn expand_contract_clause(
130130
Ok(new_tts)
131131
}
132132

133-
fn expand_requires_tts(
133+
fn expand_contract_clause_tts(
134134
ecx: &mut ExtCtxt<'_>,
135135
attr_span: Span,
136136
annotation: TokenStream,
137137
annotated: TokenStream,
138+
clause_keyword: rustc_span::Symbol,
138139
) -> Result<TokenStream, ErrorGuaranteed> {
139140
let feature_span = ecx.with_def_site_ctxt(attr_span);
140141
expand_contract_clause(ecx, attr_span, annotated, |new_tts| {
141142
new_tts.push_tree(TokenTree::Token(
142-
token::Token::from_ast_ident(Ident::new(kw::ContractRequires, feature_span)),
143-
Spacing::Joint,
144-
));
145-
new_tts.push_tree(TokenTree::Token(
146-
token::Token::new(token::TokenKind::OrOr, attr_span),
147-
Spacing::Alone,
148-
));
149-
new_tts.push_tree(TokenTree::Delimited(
150-
DelimSpan::from_single(attr_span),
151-
DelimSpacing::new(Spacing::JointHidden, Spacing::JointHidden),
152-
token::Delimiter::Brace,
153-
annotation,
154-
));
155-
Ok(())
156-
})
157-
}
158-
159-
fn expand_ensures_tts(
160-
ecx: &mut ExtCtxt<'_>,
161-
attr_span: Span,
162-
annotation: TokenStream,
163-
annotated: TokenStream,
164-
) -> Result<TokenStream, ErrorGuaranteed> {
165-
let feature_span = ecx.with_def_site_ctxt(attr_span);
166-
expand_contract_clause(ecx, attr_span, annotated, |new_tts| {
167-
new_tts.push_tree(TokenTree::Token(
168-
token::Token::from_ast_ident(Ident::new(kw::ContractEnsures, feature_span)),
143+
token::Token::from_ast_ident(Ident::new(clause_keyword, feature_span)),
169144
Spacing::Joint,
170145
));
171146
new_tts.push_tree(TokenTree::Delimited(

compiler/rustc_feature/src/removed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ declare_features! (
102102
/// Allows deriving traits as per `SmartPointer` specification
103103
(removed, derive_smart_pointer, "1.84.0", Some(123430), Some("replaced by `CoercePointee`"), 131284),
104104
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
105-
(removed, doc_auto_cfg, "CURRENT_RUSTC_VERSION", Some(43781), Some("merged into `doc_cfg`"), 138907),
105+
(removed, doc_auto_cfg, "1.92.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
106106
/// Allows `#[doc(cfg_hide(...))]`.
107-
(removed, doc_cfg_hide, "CURRENT_RUSTC_VERSION", Some(43781), Some("merged into `doc_cfg`"), 138907),
107+
(removed, doc_cfg_hide, "1.92.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
108108
/// Allows using `#[doc(keyword = "...")]`.
109109
(removed, doc_keyword, "1.58.0", Some(51315),
110110
Some("merged into `#![feature(rustdoc_internals)]`"), 90420),

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,46 +3046,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30463046
}
30473047

30483048
foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
3049-
let foreign_def_ids = foreign_preds
3050-
.iter()
3051-
.filter_map(|pred| match pred.self_ty().kind() {
3052-
ty::Adt(def, _) => Some(def.did()),
3053-
_ => None,
3054-
})
3055-
.collect::<FxIndexSet<_>>();
3056-
let mut foreign_spans: MultiSpan = foreign_def_ids
3057-
.iter()
3058-
.filter_map(|def_id| {
3059-
let span = self.tcx.def_span(*def_id);
3060-
if span.is_dummy() { None } else { Some(span) }
3061-
})
3062-
.collect::<Vec<_>>()
3063-
.into();
3064-
for pred in &foreign_preds {
3065-
if let ty::Adt(def, _) = pred.self_ty().kind() {
3066-
foreign_spans.push_span_label(
3067-
self.tcx.def_span(def.did()),
3068-
format!("not implement `{}`", pred.trait_ref.print_trait_sugared()),
3069-
);
3049+
3050+
for pred in foreign_preds {
3051+
let ty = pred.self_ty();
3052+
let ty::Adt(def, _) = ty.kind() else { continue };
3053+
let span = self.tcx.def_span(def.did());
3054+
if span.is_dummy() {
3055+
continue;
30703056
}
3071-
}
3072-
if foreign_spans.primary_span().is_some() {
3073-
let msg = if let [foreign_pred] = foreign_preds.as_slice() {
3074-
format!(
3075-
"the foreign item type `{}` doesn't implement `{}`",
3076-
foreign_pred.self_ty(),
3077-
foreign_pred.trait_ref.print_trait_sugared()
3078-
)
3079-
} else {
3080-
format!(
3081-
"the foreign item type{} {} implement required trait{} for this \
3082-
operation to be valid",
3083-
pluralize!(foreign_def_ids.len()),
3084-
if foreign_def_ids.len() > 1 { "don't" } else { "doesn't" },
3085-
pluralize!(foreign_preds.len()),
3086-
)
3087-
};
3088-
err.span_note(foreign_spans, msg);
3057+
let mut mspan: MultiSpan = span.into();
3058+
mspan.push_span_label(span, format!("`{ty}` is defined in another crate"));
3059+
err.span_note(
3060+
mspan,
3061+
format!("`{ty}` does not implement `{}`", pred.trait_ref.print_trait_sugared()),
3062+
);
30893063
}
30903064

30913065
let preds: Vec<_> = errors

compiler/rustc_index/src/idx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeInclusive<I> {
135135
}
136136
}
137137

138-
#[cfg(all(feature = "nightly", not(bootstrap)))]
138+
#[cfg(feature = "nightly")]
139139
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeToInclusive<I> {
140140
type Output = core::range::RangeToInclusive<usize>;
141141
#[inline]

compiler/rustc_index/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// tidy-alphabetical-start
22
#![cfg_attr(all(feature = "nightly", test), feature(stmt_expr_attributes))]
3-
#![cfg_attr(bootstrap, feature(new_zeroed_alloc))]
43
#![cfg_attr(feature = "nightly", allow(internal_features))]
54
#![cfg_attr(feature = "nightly", feature(extend_one, step_trait, test))]
65
#![cfg_attr(feature = "nightly", feature(new_range_api))]

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ pub(super) fn deduced_param_attrs<'tcx>(
195195

196196
// Grab the optimized MIR. Analyze it to determine which arguments have been mutated.
197197
let body: &Body<'tcx> = tcx.optimized_mir(def_id);
198+
// Arguments spread at ABI level are currently unsupported.
199+
if body.spread_arg.is_some() {
200+
return &[];
201+
}
202+
198203
let mut deduce = DeduceParamAttrs::new(body);
199204
deduce.visit_body(body);
200205
tracing::trace!(?deduce.usage);

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4036,6 +4036,30 @@ impl<'a> Parser<'a> {
40364036
self.mk_expr(span, ExprKind::Err(guar))
40374037
}
40384038

4039+
pub(crate) fn mk_unit_expr(&self, span: Span) -> Box<Expr> {
4040+
self.mk_expr(span, ExprKind::Tup(Default::default()))
4041+
}
4042+
4043+
pub(crate) fn mk_closure_expr(&self, span: Span, body: Box<Expr>) -> Box<Expr> {
4044+
self.mk_expr(
4045+
span,
4046+
ast::ExprKind::Closure(Box::new(ast::Closure {
4047+
binder: rustc_ast::ClosureBinder::NotPresent,
4048+
constness: rustc_ast::Const::No,
4049+
movability: rustc_ast::Movability::Movable,
4050+
capture_clause: rustc_ast::CaptureBy::Ref,
4051+
coroutine_kind: None,
4052+
fn_decl: Box::new(rustc_ast::FnDecl {
4053+
inputs: Default::default(),
4054+
output: rustc_ast::FnRetTy::Default(span),
4055+
}),
4056+
fn_arg_span: span,
4057+
fn_decl_span: span,
4058+
body,
4059+
})),
4060+
)
4061+
}
4062+
40394063
/// Create expression span ensuring the span of the parent node
40404064
/// is larger than the span of lhs and rhs, including the attributes.
40414065
fn mk_expr_sp(&self, lhs: &Box<Expr>, lhs_span: Span, op_span: Span, rhs_span: Span) -> Span {

0 commit comments

Comments
 (0)