Skip to content

Commit ef4c060

Browse files
committed
Address @catamorphism's comments regarding docs
1 parent 4b15bfd commit ef4c060

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,16 @@ mod guarantor {
485485
/*!
486486
*
487487
* 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).
499498
*
500499
* There are a number of troublesome scenarios in the test
501500
* `region-dependent-addr-of.rs`, but here is one example:
@@ -515,16 +514,17 @@ mod guarantor {
515514
* is "guaranteed" by a borrowed pointer, you must link the
516515
* lifetime of that borrowed pointer (L1, here) to the lifetime of
517516
* 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.
525524
*
526525
* 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.
528528
* So this is very similar logic to what you would find there,
529529
* but more special purpose.
530530
*/
@@ -540,7 +540,8 @@ mod guarantor {
540540
/*!
541541
*
542542
* 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).
544545
*/
545546

546547
debug!("guarantor::for_addr_of(base=%s)", rcx.fcx.expr_to_str(base));
@@ -555,7 +556,7 @@ mod guarantor {
555556
*
556557
* Computes the guarantors for any ref bindings in a match and
557558
* then ensures that the lifetime of the resulting pointer is
558-
* linked.
559+
* linked to the lifetime of its guarantor (if any).
559560
*/
560561

561562
let discr_guarantor = guarantor(rcx, discr);
@@ -599,13 +600,18 @@ mod guarantor {
599600
/*!
600601
*
601602
* 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).
603604
*/
604605

605606
debug!("opt_constrain_region(id=%?, guarantor=%?)", id, guarantor);
606607

607608
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+
}
609615
Some(r) => { r }
610616
};
611617

@@ -620,24 +626,38 @@ mod guarantor {
620626
}
621627
}
622628

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 {
624633
NotPointer,
625634
OwnedPointer,
626635
BorrowedPointer(ty::Region),
627636
OtherPointer
628637
}
629638

639+
/// Guarantor of an expression paired with the
640+
/// PointerCategorization` of its type.
630641
struct ExprCategorization {
631642
guarantor: Option<ty::Region>,
632-
pointer: PointerCat
643+
pointer: PointerCategorization
633644
}
634645

646+
/// ExprCategorization paired with the full type of the expr
635647
struct ExprCategorizationType {
636648
cat: ExprCategorization,
637649
ty: ty::t
638650
}
639651

640652
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+
641661
debug!("guarantor(expr=%s)", rcx.fcx.expr_to_str(expr));
642662
match expr.node {
643663
ast::expr_unary(ast::deref, b) => {
@@ -706,9 +726,7 @@ mod guarantor {
706726
}
707727
}
708728

709-
fn categorize(rcx: @rcx,
710-
expr: @ast::expr) -> ExprCategorization
711-
{
729+
fn categorize(rcx: @rcx, expr: @ast::expr) -> ExprCategorization {
712730
debug!("categorize(expr=%s)", rcx.fcx.expr_to_str(expr));
713731
let _i = ::util::common::indenter();
714732

@@ -722,6 +740,8 @@ mod guarantor {
722740
rcx, expr, adjustment.autoderefs, expr_ct);
723741

724742
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.
725745
expr_ct.cat.guarantor = None;
726746
expr_ct.cat.pointer = BorrowedPointer(autoref.region);
727747
debug!("autoref, cat=%?", expr_ct.cat);
@@ -784,7 +804,7 @@ mod guarantor {
784804
return ct;
785805
}
786806

787-
fn pointer_categorize(ty: ty::t) -> PointerCat {
807+
fn pointer_categorize(ty: ty::t) -> PointerCategorization {
788808
match ty::get(ty).sty {
789809
ty::ty_rptr(r, _) | ty::ty_evec(_, ty::vstore_slice(r)) |
790810
ty::ty_estr(ty::vstore_slice(r)) => {
@@ -911,7 +931,7 @@ fn infallibly_mk_subr(rcx: @rcx,
911931
{
912932
/*!
913933
*
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
915935
* know that this can never yield an error due to the way that
916936
* region inferencing works. Therefore just report a bug if we
917937
* ever see `Err(_)`.

0 commit comments

Comments
 (0)