Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 59642ca

Browse files
committedOct 15, 2024
checkpoint something else that builds.
1 parent e8b3a46 commit 59642ca

File tree

11 files changed

+67
-27
lines changed

11 files changed

+67
-27
lines changed
 

‎compiler/rustc_ast/src/visit.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl BoundKind {
6666
#[derive(Copy, Clone, Debug)]
6767
pub enum FnKind<'a> {
6868
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
69-
Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
69+
Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, &'a FnContract, Option<&'a Block>),
7070

7171
/// E.g., `|x, y| body`.
7272
Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
@@ -75,7 +75,7 @@ pub enum FnKind<'a> {
7575
impl<'a> FnKind<'a> {
7676
pub fn header(&self) -> Option<&'a FnHeader> {
7777
match *self {
78-
FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
78+
FnKind::Fn(_, _, sig, _, _, _, _) => Some(&sig.header),
7979
FnKind::Closure(..) => None,
8080
}
8181
}
@@ -89,7 +89,7 @@ impl<'a> FnKind<'a> {
8989

9090
pub fn decl(&self) -> &'a FnDecl {
9191
match self {
92-
FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
92+
FnKind::Fn(_, _, sig, _, _, _, _) => &sig.decl,
9393
FnKind::Closure(_, _, decl, _) => decl,
9494
}
9595
}
@@ -363,8 +363,8 @@ impl WalkItemKind for ItemKind {
363363
visit_opt!(visitor, visit_expr, expr);
364364
}
365365
ItemKind::Fn(box Fn { defaultness: _, generics, sig, contract, body }) => {
366-
let kind = FnKind::Fn(FnCtxt::Free, *ident, sig, vis, generics, body.as_deref());
367-
try_visit!(visitor.visit_contract(contract));
366+
let kind = FnKind::Fn(FnCtxt::Free, *ident, sig, vis, generics, &contract, body.as_deref());
367+
// try_visit!(visitor.visit_contract(contract));
368368
try_visit!(visitor.visit_fn(kind, *span, *id));
369369
}
370370
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
@@ -700,8 +700,8 @@ impl WalkItemKind for ForeignItemKind {
700700
visit_opt!(visitor, visit_expr, expr);
701701
}
702702
ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, contract, body }) => {
703-
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
704-
try_visit!(visitor.visit_contract(contract));
703+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, contract, body.as_deref());
704+
// try_visit!(visitor.visit_contract(contract));
705705
try_visit!(visitor.visit_fn(kind, span, id));
706706
}
707707
ForeignItemKind::TyAlias(box TyAlias {
@@ -849,11 +849,15 @@ pub fn walk_fn_decl<'a, V: Visitor<'a>>(
849849

850850
pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Result {
851851
match kind {
852-
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span: _ }, _vis, generics, body) => {
852+
FnKind::Fn(_ctxt, _ident,
853+
FnSig { header, decl, span: _ },
854+
_vis, generics, contract, body,
855+
) => {
853856
// Identifier and visibility are visited as a part of the item.
854857
try_visit!(visitor.visit_fn_header(header));
855858
try_visit!(visitor.visit_generics(generics));
856859
try_visit!(walk_fn_decl(visitor, decl));
860+
try_visit!(visitor.visit_contract(contract));
857861
visit_opt!(visitor, visit_block, body);
858862
}
859863
FnKind::Closure(binder, _coroutine_kind, decl, body) => {
@@ -882,7 +886,7 @@ impl WalkItemKind for AssocItemKind {
882886
AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, contract, body }) => {
883887
try_visit!(visitor.visit_contract(contract));
884888
let kind =
885-
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
889+
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, contract, body.as_deref());
886890
try_visit!(visitor.visit_fn(kind, span, id));
887891
}
888892
AssocItemKind::Type(box TyAlias {

‎compiler/rustc_ast_passes/src/ast_validation.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -989,9 +989,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
989989

990990
self.visit_vis(&item.vis);
991991
self.visit_ident(item.ident);
992-
self.visit_contract(contract);
992+
// self.visit_contract(contract);
993993
let kind =
994-
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
994+
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, contract, body.as_deref());
995995
self.visit_fn(kind, item.span, item.id);
996996
walk_list!(self, visit_attribute, &item.attrs);
997997
return; // Avoid visiting again.
@@ -1397,13 +1397,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13971397
_,
13981398
_,
13991399
_,
1400+
_,
14001401
) = fk
14011402
{
14021403
self.maybe_lint_missing_abi(*sig_span, id);
14031404
}
14041405

14051406
// Functions without bodies cannot have patterns.
1406-
if let FnKind::Fn(ctxt, _, sig, _, _, None) = fk {
1407+
if let FnKind::Fn(ctxt, _, sig, _, _, _, None) = fk {
14071408
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
14081409
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
14091410
if let Some(ident) = ident {
@@ -1437,7 +1438,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14371438
.is_some();
14381439

14391440
let disallowed = (!tilde_const_allowed).then(|| match fk {
1440-
FnKind::Fn(_, ident, _, _, _, _) => TildeConstReason::Function { ident: ident.span },
1441+
FnKind::Fn(_, ident, _, _, _, _, _) => TildeConstReason::Function { ident: ident.span },
14411442
FnKind::Closure(..) => TildeConstReason::Closure,
14421443
});
14431444
self.with_tilde_const(disallowed, |this| visit::walk_fn(this, fk));
@@ -1513,7 +1514,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15131514
self.outer_trait_or_trait_impl.as_ref().and_then(TraitOrTraitImpl::constness).is_some();
15141515

15151516
match &item.kind {
1516-
AssocItemKind::Fn(box Fn { sig, generics, body, .. })
1517+
AssocItemKind::Fn(box Fn { sig, generics, contract, body, .. })
15171518
if parent_is_const
15181519
|| ctxt == AssocCtxt::Trait
15191520
|| matches!(sig.header.constness, Const::Yes(_)) =>
@@ -1526,6 +1527,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15261527
sig,
15271528
&item.vis,
15281529
generics,
1530+
contract,
15291531
body.as_deref(),
15301532
);
15311533
walk_list!(self, visit_attribute, &item.attrs);

‎compiler/rustc_lint/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ impl EarlyLintPass for UnsafeCode {
349349
ast::FnSig { header: ast::FnHeader { safety: ast::Safety::Unsafe(_), .. }, .. },
350350
_,
351351
_,
352+
_,
352353
body,
353354
) = fk
354355
{

‎compiler/rustc_lint/src/early.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
157157

158158
// Explicitly check for lints associated with 'closure_id', since
159159
// it does not have a corresponding AST node
160-
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk {
160+
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _, _) = fk {
161161
if let Some(coroutine_kind) = sig.header.coroutine_kind {
162162
self.check_id(coroutine_kind.closure_id());
163163
}

‎compiler/rustc_resolve/src/def_collector.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,14 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
240240

241241
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
242242
match fn_kind {
243-
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span: _ }, _vis, generics, body)
243+
FnKind::Fn(_ctxt, _ident,
244+
FnSig { header, decl, span: _ },
245+
_vis, generics, contract, body)
244246
if let Some(coroutine_kind) = header.coroutine_kind =>
245247
{
246248
self.visit_fn_header(header);
247249
self.visit_generics(generics);
250+
self.visit_contract(contract); // XXX
248251

249252
// For async functions, we need to create their inner defs inside of a
250253
// closure to match their desugared representation. Besides that,

‎compiler/rustc_resolve/src/late.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,8 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
961961
match fn_kind {
962962
// Bail if the function is foreign, and thus cannot validly have
963963
// a body, or if there's no body for some other reason.
964-
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _)
965-
| FnKind::Fn(_, _, sig, _, generics, None) => {
964+
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _, _)
965+
| FnKind::Fn(_, _, sig, _, generics, _, None) => {
966966
self.visit_fn_header(&sig.header);
967967
self.visit_generics(generics);
968968
self.with_lifetime_rib(
@@ -1002,7 +1002,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
10021002
// Create a label rib for the function.
10031003
this.with_label_rib(RibKind::FnOrCoroutine, |this| {
10041004
match fn_kind {
1005-
FnKind::Fn(_, _, sig, _, generics, body) => {
1005+
FnKind::Fn(_, _, sig, _, generics, contract, body) => {
10061006
this.visit_generics(generics);
10071007

10081008
let declaration = &sig.decl;
@@ -1033,6 +1033,8 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
10331033
},
10341034
);
10351035

1036+
this.visit_contract(contract);
1037+
10361038
if let Some(body) = body {
10371039
// Ignore errors in function bodies if this is rustdoc
10381040
// Be sure not to set this until the function signature has been resolved.

‎compiler/rustc_resolve/src/late/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3169,7 +3169,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
31693169
{
31703170
let pre = if lt.kind == MissingLifetimeKind::Ampersand
31713171
&& let Some((kind, _span)) = self.diag_metadata.current_function
3172-
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
3172+
&& let FnKind::Fn(_, _, sig, _, _, _, _) = kind
31733173
&& !sig.decl.inputs.is_empty()
31743174
&& let sugg = sig
31753175
.decl
@@ -3210,7 +3210,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32103210
} else if (lt.kind == MissingLifetimeKind::Ampersand
32113211
|| lt.kind == MissingLifetimeKind::Underscore)
32123212
&& let Some((kind, _span)) = self.diag_metadata.current_function
3213-
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
3213+
&& let FnKind::Fn(_, _, sig, _, _, _, _) = kind
32143214
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
32153215
&& !sig.decl.inputs.is_empty()
32163216
&& let arg_refs = sig
@@ -3270,7 +3270,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32703270
let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
32713271
let mut sugg = vec![(lt.span, String::new())];
32723272
if let Some((kind, _span)) = self.diag_metadata.current_function
3273-
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
3273+
&& let FnKind::Fn(_, _, sig, _, _, _, _) = kind
32743274
&& let ast::FnRetTy::Ty(ty) = &sig.decl.output
32753275
{
32763276
let mut lt_finder =

‎src/tools/clippy/clippy_lints/src/multiple_bound_locations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare_lint_pass!(MultipleBoundLocations => [MULTIPLE_BOUND_LOCATIONS]);
3939

4040
impl EarlyLintPass for MultipleBoundLocations {
4141
fn check_fn(&mut self, cx: &EarlyContext<'_>, kind: FnKind<'_>, _: Span, _: NodeId) {
42-
if let FnKind::Fn(_, _, _, _, generics, _) = kind
42+
if let FnKind::Fn(_, _, _, _, generics, _, _) = kind
4343
&& !generics.params.is_empty()
4444
&& !generics.where_clause.predicates.is_empty()
4545
{

‎src/tools/clippy/clippy_utils/src/ast_utils.rs

+23
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,21 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
362362
defaultness: ld,
363363
sig: lf,
364364
generics: lg,
365+
contract: lc,
365366
body: lb,
366367
}),
367368
Fn(box ast::Fn {
368369
defaultness: rd,
369370
sig: rf,
370371
generics: rg,
372+
contract: rc,
371373
body: rb,
372374
}),
373375
) => {
374376
eq_defaultness(*ld, *rd)
375377
&& eq_fn_sig(lf, rf)
376378
&& eq_generics(lg, rg)
379+
&& eq_contracts(lc, rc)
377380
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
378381
},
379382
(Mod(lu, lmk), Mod(ru, rmk)) => {
@@ -497,18 +500,21 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
497500
defaultness: ld,
498501
sig: lf,
499502
generics: lg,
503+
contract: lc,
500504
body: lb,
501505
}),
502506
Fn(box ast::Fn {
503507
defaultness: rd,
504508
sig: rf,
505509
generics: rg,
510+
contract: rc,
506511
body: rb,
507512
}),
508513
) => {
509514
eq_defaultness(*ld, *rd)
510515
&& eq_fn_sig(lf, rf)
511516
&& eq_generics(lg, rg)
517+
&& eq_contracts(lc, rc)
512518
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
513519
},
514520
(
@@ -559,18 +565,21 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
559565
defaultness: ld,
560566
sig: lf,
561567
generics: lg,
568+
contract: lc,
562569
body: lb,
563570
}),
564571
Fn(box ast::Fn {
565572
defaultness: rd,
566573
sig: rf,
567574
generics: rg,
575+
contract: rc,
568576
body: rb,
569577
}),
570578
) => {
571579
eq_defaultness(*ld, *rd)
572580
&& eq_fn_sig(lf, rf)
573581
&& eq_generics(lg, rg)
582+
&& eq_contracts(lc, rc)
574583
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
575584
},
576585
(
@@ -677,6 +686,20 @@ pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
677686
}
678687
}
679688

689+
pub fn eq_contracts(l: &FnContract, r: &FnContract) -> bool {
690+
let req_cmp = match (&l.requires, &r.requires) {
691+
(Some(l_expr), Some(r_expr)) => eq_expr(l_expr, r_expr),
692+
(None, None) => true,
693+
_ => return false,
694+
};
695+
let ens_cmp = match (&l.ensures, &r.ensures) {
696+
(Some(l_expr), Some(r_expr)) => eq_expr(l_expr, r_expr),
697+
(None, None) => true,
698+
_ => return false,
699+
};
700+
req_cmp && ens_cmp
701+
}
702+
680703
pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
681704
eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
682705
}

‎src/tools/rustfmt/src/items.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ impl<'a> FnSig<'a> {
333333
defaultness: ast::Defaultness,
334334
) -> FnSig<'a> {
335335
match *fn_kind {
336-
visit::FnKind::Fn(visit::FnCtxt::Assoc(..), _, fn_sig, vis, generics, _) => {
336+
visit::FnKind::Fn(visit::FnCtxt::Assoc(..), _, fn_sig, vis, generics, _, _) => {
337337
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis);
338338
fn_sig.defaultness = defaultness;
339339
fn_sig
340340
}
341-
visit::FnKind::Fn(_, _, fn_sig, vis, generics, _) => FnSig {
341+
visit::FnKind::Fn(_, _, fn_sig, vis, generics, _, _) => FnSig {
342342
decl,
343343
generics,
344344
ext: fn_sig.header.ext,
@@ -3432,6 +3432,7 @@ impl Rewrite for ast::ForeignItem {
34323432
defaultness,
34333433
ref sig,
34343434
ref generics,
3435+
ref contract,
34353436
ref body,
34363437
} = **fn_kind;
34373438
if let Some(ref body) = body {
@@ -3447,6 +3448,7 @@ impl Rewrite for ast::ForeignItem {
34473448
sig,
34483449
&self.vis,
34493450
generics,
3451+
contract,
34503452
Some(body),
34513453
),
34523454
&sig.decl,

‎src/tools/rustfmt/src/visitor.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
386386
let indent = self.block_indent;
387387
let block;
388388
let rewrite = match fk {
389-
visit::FnKind::Fn(_, ident, _, _, _, Some(ref b)) => {
389+
visit::FnKind::Fn(_, ident, _, _, _, _, Some(ref b)) => {
390390
block = b;
391391
self.rewrite_fn_before_block(
392392
indent,
@@ -538,6 +538,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
538538
defaultness,
539539
ref sig,
540540
ref generics,
541+
ref contract,
541542
ref body,
542543
} = **fn_kind;
543544
if let Some(ref body) = body {
@@ -553,6 +554,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
553554
sig,
554555
&item.vis,
555556
generics,
557+
contract,
556558
Some(body),
557559
),
558560
&sig.decl,
@@ -646,13 +648,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
646648
defaultness,
647649
ref sig,
648650
ref generics,
651+
ref contract,
649652
ref body,
650653
} = **fn_kind;
651654
if let Some(ref body) = body {
652655
let inner_attrs = inner_attributes(&ai.attrs);
653656
let fn_ctxt = visit::FnCtxt::Assoc(assoc_ctxt);
654657
self.visit_fn(
655-
visit::FnKind::Fn(fn_ctxt, ai.ident, sig, &ai.vis, generics, Some(body)),
658+
visit::FnKind::Fn(fn_ctxt, ai.ident, sig, &ai.vis, generics, contract, Some(body)),
656659
&sig.decl,
657660
ai.span,
658661
defaultness,

0 commit comments

Comments
 (0)
Please sign in to comment.