@@ -485,17 +485,16 @@ mod guarantor {
485
485
/*!
486
486
*
487
487
* The routines in this module are aiming to deal with the case
488
- * where the lifetime resulting from a borrow is linked to the
489
- * lifetime of the thing being borrowed. Imagine you have a
490
- * borrowed pointer `b` with lifetime L1 and you have an
491
- * expression `&*b`. The result of this borrow will be another
492
- * borrowed pointer with lifetime L2 (which is an inference
493
- * variable). The borrow checker is going to enforce the
494
- * constraint that L2 < L1, because otherwise you are re-borrowing
495
- * data for a lifetime larger than the original loan. However,
496
- * without the routines in this module, the region inferencer would
497
- * not know of this dependency and thus it might infer the
498
- * lifetime of L2 to be greater than L1 (issue #3148).
488
+ * where a the contents of a borrowed pointer are re-borrowed.
489
+ * Imagine you have a borrowed pointer `b` with lifetime L1 and
490
+ * you have an expression `&*b`. The result of this borrow will
491
+ * be another borrowed pointer with lifetime L2 (which is an
492
+ * inference variable). The borrow checker is going to enforce
493
+ * the constraint that L2 < L1, because otherwise you are
494
+ * re-borrowing data for a lifetime larger than the original loan.
495
+ * However, without the routines in this module, the region
496
+ * inferencer would not know of this dependency and thus it might
497
+ * infer the lifetime of L2 to be greater than L1 (issue #3148).
499
498
*
500
499
* There are a number of troublesome scenarios in the test
501
500
* `region-dependent-addr-of.rs`, but here is one example:
@@ -515,16 +514,17 @@ mod guarantor {
515
514
* is "guaranteed" by a borrowed pointer, you must link the
516
515
* lifetime of that borrowed pointer (L1, here) to the lifetime of
517
516
* the borrow itself (L2). What do I mean by "guaranteed" by a
518
- * borrowed pointer? Well, I would say the data "owned" by the
519
- * borrowed pointer, except that a borrowed pointer never owns its
520
- * contents, but the relation is the same . That is, I mean any
521
- * data that is reached by first derefencing a borrowed pointer
522
- * and then either traversing interior offsets or owned pointers.
523
- * We say that the guarantor of such data it the region of the borrowed
524
- * pointer that was traversed .
517
+ * borrowed pointer? I mean any data that is reached by first
518
+ * dereferencing a borrowed pointer and then either traversing
519
+ * interior offsets or owned pointers . We say that the guarantor
520
+ * of such data it the region of the borrowed pointer that was
521
+ * traversed. This is essentially the same as the ownership
522
+ * relation, except that a borrowed pointer never owns its
523
+ * contents .
525
524
*
526
525
* NB: I really wanted to use the `mem_categorization` code here
527
- * but I cannot because final type resolution hasn't happened yet.
526
+ * but I cannot because final type resolution hasn't happened yet,
527
+ * and `mem_categorization` requires that all types be known.
528
528
* So this is very similar logic to what you would find there,
529
529
* but more special purpose.
530
530
*/
@@ -540,7 +540,8 @@ mod guarantor {
540
540
/*!
541
541
*
542
542
* Computes the guarantor for an expression `&base` and then
543
- * ensures that the lifetime of the resulting pointer is linked.
543
+ * ensures that the lifetime of the resulting pointer is linked
544
+ * to the lifetime of its guarantor (if any).
544
545
*/
545
546
546
547
debug!( "guarantor:: for_addr_of( base=%s) ", rcx. fcx. expr_to_str( base) ) ;
@@ -555,7 +556,7 @@ mod guarantor {
555
556
*
556
557
* Computes the guarantors for any ref bindings in a match and
557
558
* then ensures that the lifetime of the resulting pointer is
558
- * linked.
559
+ * linked to the lifetime of its guarantor (if any) .
559
560
*/
560
561
561
562
let discr_guarantor = guarantor( rcx, discr) ;
@@ -599,13 +600,18 @@ mod guarantor {
599
600
/*!
600
601
*
601
602
* Links the lifetime of the borrowed pointer resulting from a borrow
602
- * to the lifetime of its guarantor.
603
+ * to the lifetime of its guarantor (if any) .
603
604
*/
604
605
605
606
debug!( "opt_constrain_region( id=%?, guarantor=%?) ", id, guarantor) ;
606
607
607
608
let bound = match guarantor {
608
- None => { return ; }
609
+ None => {
610
+ // If guarantor is None, then the value being borrowed
611
+ // is not guaranteed by a region pointer, so there are
612
+ // no lifetimes to link.
613
+ return ;
614
+ }
609
615
Some ( r) => { r }
610
616
} ;
611
617
@@ -620,24 +626,38 @@ mod guarantor {
620
626
}
621
627
}
622
628
623
- enum PointerCat {
629
+ /// Categorizes types based on what kind of pointer they are.
630
+ /// Note that we don't bother to distinguish between rptrs (&T)
631
+ /// and slices (&[T], &str)---they are all just `BorrowedPointer`.
632
+ enum PointerCategorization {
624
633
NotPointer ,
625
634
OwnedPointer ,
626
635
BorrowedPointer ( ty:: Region ) ,
627
636
OtherPointer
628
637
}
629
638
639
+ /// Guarantor of an expression paired with the
640
+ /// PointerCategorization` of its type.
630
641
struct ExprCategorization {
631
642
guarantor: Option <ty:: Region >,
632
- pointer: PointerCat
643
+ pointer: PointerCategorization
633
644
}
634
645
646
+ /// ExprCategorization paired with the full type of the expr
635
647
struct ExprCategorizationType {
636
648
cat: ExprCategorization ,
637
649
ty: ty:: t
638
650
}
639
651
640
652
fn guarantor( rcx: @rcx, expr: @ast:: expr) -> Option <ty:: Region > {
653
+ /*!
654
+ *
655
+ * Computes the guarantor of `expr`, or None if `expr` is
656
+ * not guaranteed by any region. Here `expr` is some expression
657
+ * whose address is being taken (e.g., there is an expression
658
+ * `&expr`).
659
+ */
660
+
641
661
debug!( "guarantor( expr=%s) ", rcx. fcx. expr_to_str( expr) ) ;
642
662
match expr. node {
643
663
ast:: expr_unary( ast:: deref, b) => {
@@ -706,9 +726,7 @@ mod guarantor {
706
726
}
707
727
}
708
728
709
- fn categorize( rcx: @rcx,
710
- expr: @ast:: expr) -> ExprCategorization
711
- {
729
+ fn categorize( rcx: @rcx, expr: @ast:: expr) -> ExprCategorization {
712
730
debug!( "categorize( expr=%s) ", rcx. fcx. expr_to_str( expr) ) ;
713
731
let _i = :: util:: common:: indenter( ) ;
714
732
@@ -722,6 +740,8 @@ mod guarantor {
722
740
rcx, expr, adjustment. autoderefs, expr_ct) ;
723
741
724
742
for adjustment. autoref. each |autoref| {
743
+ // If there is an autoref, then the result of this
744
+ // expression will be some sort of borrowed pointer.
725
745
expr_ct. cat. guarantor = None ;
726
746
expr_ct. cat. pointer = BorrowedPointer ( autoref. region) ;
727
747
debug!( "autoref, cat=%?", expr_ct. cat) ;
@@ -784,7 +804,7 @@ mod guarantor {
784
804
return ct;
785
805
}
786
806
787
- fn pointer_categorize( ty: ty:: t) -> PointerCat {
807
+ fn pointer_categorize( ty: ty:: t) -> PointerCategorization {
788
808
match ty:: get( ty) . sty {
789
809
ty:: ty_rptr( r, _) | ty:: ty_evec( _, ty:: vstore_slice( r) ) |
790
810
ty:: ty_estr( ty:: vstore_slice( r) ) => {
@@ -911,7 +931,7 @@ fn infallibly_mk_subr(rcx: @rcx,
911
931
{
912
932
/*!
913
933
*
914
- * Constraints `a` to be a subregion of `b`. In many cases, we
934
+ * Constrains `a` to be a subregion of `b`. In many cases, we
915
935
* know that this can never yield an error due to the way that
916
936
* region inferencing works. Therefore just report a bug if we
917
937
* ever see `Err(_)`.
0 commit comments