Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend E0623 for LateBound and EarlyBound Regions #44079

Merged
merged 2 commits into from
Sep 10, 2017

Conversation

gaurikholkar-zz
Copy link

@gaurikholkar-zz gaurikholkar-zz commented Aug 25, 2017

This is a fix for #43882

fn foo<'a,'b>(x: &mut Vec<&'a u8>, y: &'b u8) {
    x.push(y);
}

now gives

error[E0623]: lifetime mismatch
  --> $DIR/ex3-both-anon-regions-latebound-regions.rs:12:12
   |
11 | fn foo<'a,'b>(x: &mut Vec<&'a u8>, y: &'b u8) {
   |                           ------      ------ these two types are declared with different lifetimes...
12 |     x.push(y);
   |            ^ ...but data from `y` flows into `x` here

cc @nikomatsakis @arielb1

Please ignore the second commit. It will be merged in a separate PR.

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@gaurikholkar-zz gaurikholkar-zz force-pushed the named_conf branch 3 times, most recently from 5823310 to 3665a7f Compare August 25, 2017 09:04
@bors
Copy link
Contributor

bors commented Aug 25, 2017

☔ The latest upstream changes (presumably #43700) made this pull request unmergeable. Please resolve the merge conflicts.

@shepmaster
Copy link
Member

Please ignore the second commit. It will be merged in a separate PR.

Based on this and the merge conflicts, I'm going to treat this as a kind of WIP.

@shepmaster shepmaster added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 25, 2017
@gaurikholkar-zz
Copy link
Author

@shepmaster the first commit was #43700 itself which just got merged. I'll fix the merge conflicts and the compile - tests now

@gaurikholkar-zz gaurikholkar-zz changed the title Extend E0623 for LateBound Regions Extend E0623 for LateBound and EarlyBound Regions Aug 26, 2017
@gaurikholkar-zz gaurikholkar-zz force-pushed the named_conf branch 2 times, most recently from 6cdae88 to a944650 Compare August 27, 2017 17:18
@gaurikholkar-zz
Copy link
Author

gaurikholkar-zz commented Aug 27, 2017

@arielb1 can you review this please. The error code works fine for LateBound, not working yet for EarlyBound.

@gaurikholkar-zz
Copy link
Author

@nikomatsakis can you please review the compile-fail tests?

if debruijn_index.depth == 1 && anon_index == br_index {
self.found_type = Some(arg);
return; // we can stop visiting now
match self.bound_region {
Copy link
Contributor

@arielb1 arielb1 Aug 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: you could do a

match (self.infcx.tcx.named_region_map.defs.get(&lifetime.id),
       self.bound_region) {
    (Some(&rl::Region::LateBoundAnon(debruijn_index, anon_index)),
     ty::BrAnon(br_index)) => {
        // ..
    }
    // ..
}

}
return true;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: no newline at end of file

.as_local_node_id(suitable_region_binding_scope)
.unwrap();
let mut is_impl_item = false;
match self.tcx.hir.find(node_id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style:

let is_impl_item = match self.tcx.hir.find(node_id) {
    Some(hir_map::NodeItem(..)) |
    Some(hir_map::NodeTraitItem(..)) => {
        false
    }
    Some(hir_map::NodeImplItem(..)) => {
        self.is_bound_region_in_impl_item(suitable_region_binding_scope)
    }
    _ => return None
};

@@ -40,7 +40,6 @@ fn with_assoc<'a,'b>() {
// We get an error because 'b:'a does not hold:

let _: &'a WithHrAssoc<TheType<'b>> = loop { };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did the error go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this code compile with your patch? Could you try to figure out why an error is not generated?

}

fn call<T>() { }

fn main() {
}
}p
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray p in program

| ^ ...but data from `y` flows into `x` here

error: aborting due to previous error

Copy link
Contributor

@arielb1 arielb1 Aug 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a test for early-bound regions (using the 'static: 'a, 'static: 'b trick?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example we had discussed with where clause and sized trait doesn't seem to be working. So.still trying to get the code working for early bound

@arielb1
Copy link
Contributor

arielb1 commented Aug 28, 2017

I would say r=me after style fixes, but

WTF is happening with the error messages?

@gaurikholkar-zz
Copy link
Author

@arielb some of the tests keep failing, I fixed them but they failed again.

@arielb1
Copy link
Contributor

arielb1 commented Aug 28, 2017

@gaurikholkar

Are you on IRC?

@gaurikholkar-zz
Copy link
Author

Afk for a few hours. @gkholkar on irc.

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments. This looks pretty nice altogether, though I am concerned about the "disappearing errors" that @arielb1 noted. That said, I see travis isn't passing, so maybe those errors are still being generated?

@@ -46,9 +46,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
};

// Determine whether the sub and sup consist of both anonymous (elided) regions.
let anon_reg_sup = or_false!(self.is_suitable_anonymous_region(sup));
let anon_reg_sup = or_false!(self.is_suitable_region(sup));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rename this variable, if it's not always anonymous? (For that matter, this file and fn?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about both_params_anon_or_named_conflict.rs ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, maybe something like different_lifetimes.rs? (since this generates the "types declared with different lifetimes..." message)

@@ -193,4 +191,4 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
false
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: newline at end of file

@@ -47,7 +47,7 @@ fn with_assoc<'a,'b>() {
// outlive 'a. In this case, that means TheType<'b>::TheAssocType,
// which is &'b (), must outlive 'a.

let _: &'a WithAssoc<TheType<'b>> = loop { }; //~ ERROR reference has a longer lifetime
let _: &'a WithAssoc<TheType<'b>> = loop { };//~ ERROR reference has a longer lifetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: no need to remove this space, I'd say...

@nikomatsakis
Copy link
Contributor

Status update: doesn't fully work for early bound regions yet. Have to investigate to see why.

@gaurikholkar-zz gaurikholkar-zz force-pushed the named_conf branch 2 times, most recently from 5442498 to ef2f0e1 Compare September 2, 2017 14:54
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lookin' good! Left a few more nits. We have to solve the disappearing error problem, obviously.

pub fn report_region_errors(&self, errors: &Vec<RegionResolutionError<'tcx>>) {
debug!("report_region_errors(): {} errors to start", errors.len());

// try to pre-process the errors, which will group some of them
// together into a `ProcessedErrors` group:
let errors = self.process_errors(errors);

debug!("report_region_errors: {} errors after preprocessing", errors.len());
debug!("report_region_errors: {} errors after preprocessing",
errors.len());

for error in errors {
debug!("report_region_errors: error = {:?}", error);

if !self.try_report_named_anon_conflict(&error) &&
!self.try_report_anon_anon_conflict(&error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should rename try_report_anon_anon_conflict() to try_report_different_lifetime_conflict()

// and returns the DefId and the BoundRegion corresponding to the given region.
pub fn is_suitable_anonymous_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
// This method returns the DefId and the BoundRegion corresponding to the given region.
pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can call this is_suitable_free_region()? Seems a touch more informative.

@@ -15,7 +15,7 @@ fn a<'a, 'b>(x: &mut &'a isize, y: &mut &'b isize) where 'b: 'a {

fn b<'a, 'b>(x: &mut &'a isize, y: &mut &'b isize) {
// Illegal now because there is no `'b:'a` declaration.
*x = *y; //~ ERROR E0312
*x = *y; //~ ERROR E0623
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice =)

@@ -15,20 +15,16 @@
struct Paramd<'a> { x: &'a usize }

fn call2<'a, 'b>(a: &'a usize, b: &'b usize) {
let z: Option<&'b &'a usize> = None;
//~^ ERROR reference has a longer lifetime than the data it references
let z: Option<&'b &'a usize> = None;//~ ERROR E0623
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that the error in this case could be a touch misleading -- we'll say that "data flows from a into b here" or whatever, when in fact no dataflow actually occurs. This seems kind of like an edge case though. I'm inclined to leave it as is for now. We may want to reconsider in the future though. Maybe worth opening an issue about at some point.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -40,7 +40,6 @@ fn with_assoc<'a,'b>() {
// We get an error because 'b:'a does not hold:

let _: &'a WithHrAssoc<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, this error should not disappear -- when I investigated a bit, it appeared we were ICEing...?

@@ -61,7 +60,7 @@ fn with_assoc_sub<'a,'b>() {
// below to be well-formed, it is not related to the HR relation.

let _: &'a WithHrAssocSub<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error should not disappear

@@ -42,7 +42,7 @@ fn with_assoc<'a,'b>() {
// which is &'b (), must outlive 'a.

let _: &'a WithAssoc<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error should not disappear

}

fn call_without_assoc<'a,'b>() {
// As `without_assoc`, but in a distinct scenario.

call::<&'a WithoutAssoc<TheType<'b>>>(); //~ ERROR reference has a longer lifetime
call::<&'a WithoutAssoc<TheType<'b>>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error should not disappear

@gaurikholkar-zz
Copy link
Author

@GuillaumeGomez, changed the diagnostics.rs for E0312

@@ -1383,38 +1383,6 @@ struct Foo<T: 'static> {
```
"##,

E0312: r##"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error E0312 still exists so why not writing its long error diagnostic instead of just removing the old one?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At it. Just thought you should know.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 8, 2017

📌 Commit 1c304d2 has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Sep 8, 2017

🔒 Merge conflict

@bors
Copy link
Contributor

bors commented Sep 8, 2017

☔ The latest upstream changes (presumably #44142) made this pull request unmergeable. Please resolve the merge conflicts.

@gaurikholkar-zz gaurikholkar-zz force-pushed the named_conf branch 2 times, most recently from f91c3fe to f2ac5dd Compare September 8, 2017 16:55
@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 8, 2017

📌 Commit f2ac5dd has been approved by nikomatsakis

@vitiral
Copy link
Contributor

vitiral commented Sep 8, 2017

I was directed here from this comment.

I may be missing what this message is trying to say, but it looks like the compiler should suggest an "outlives" annotation in this case.

^ ...but data from `y` flows into `x` here`

The above message doesn't tell me what to do. I believe it is saying that foo needs to be declared as:

fn foo<'a, 'b: 'a>

since 'b needs to outlive 'a.

Could this be modified to do so, or a new issue opened to add the Help message?

@gaurikholkar-zz gaurikholkar-zz force-pushed the named_conf branch 2 times, most recently from 1770e90 to 9caab73 Compare September 9, 2017 03:30
@gaurikholkar-zz gaurikholkar-zz changed the title Extend E0623 for LateBound and EarlyBound Regions WIP Extend E0623 for LateBound and EarlyBound Regions Sep 9, 2017
@nikomatsakis
Copy link
Contributor

@vitiral

The above message doesn't tell me what to do

Thanks for the suggestion. =) This is tricky. This message is intended, in part, as a fallback message -- with the extended error (--explain) giving advice on possible remedies. On of the bits of follow-up work we intended was to give a more concrete suggestion for how to fix things -- including possibly adding a where-clause. But that is not always appropriate (e.g., if you are implementing a trait, you may not be at liberty to add a where-clause, unless you an also modify the trait). So yes, I think opening up a follow-up issue is the way to go -- I forget if we have already done so or not, but it was the plan.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 10, 2017

📌 Commit 88e4bf6 has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Sep 10, 2017

⌛ Testing commit 88e4bf6 with merge b413f34...

bors added a commit that referenced this pull request Sep 10, 2017
Extend E0623 for LateBound and EarlyBound Regions

This is a fix for #43882
```
fn foo<'a,'b>(x: &mut Vec<&'a u8>, y: &'b u8) {
    x.push(y);
}
```
now gives

```
error[E0623]: lifetime mismatch
  --> $DIR/ex3-both-anon-regions-latebound-regions.rs:12:12
   |
11 | fn foo<'a,'b>(x: &mut Vec<&'a u8>, y: &'b u8) {
   |                           ------      ------ these two types are declared with different lifetimes...
12 |     x.push(y);
   |            ^ ...but data from `y` flows into `x` here
```
cc @nikomatsakis @arielb1

Please ignore the second commit. It will be merged in a separate PR.
@bors
Copy link
Contributor

bors commented Sep 10, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing b413f34 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants