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

improve case with both anonymous lifetime parameters in Structs #43275

Closed
gaurikholkar-zz opened this issue Jul 16, 2017 · 7 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@gaurikholkar-zz
Copy link

Generate error message for the case where , for structs, we have missing lifetime parameters.
For e.g. a sample error message

struct Ref<'a> { x: &'a u32 }

fn foo(mut x: Vec<Ref>, y: Ref) {
                  ---      --- must have the same lifetime
  x.push(y);
|            ^ data from `y` flows into `x` here

}

There are cases where more than one lifetime parameters come into play.

struct Ref<'a, 'b> { a: &'a u32, b: &'b u32 }

fn foo(mut x: Ref, y: Ref) {
              ---     --- 2nd lifetime parameter on `Ref` must match
              2nd lifetime parameter on `Ref` must match
  x.b = y.b;
}

struct Ref<'a, 'b> { a: &'a u32, b: &'b u32 }

fn foo<'x1, 'x2, 'y1>(mut x: Ref<'x1, 'x2>, y: Ref<'y1, 'y2>) {
  x.b = y.b;
}

cc @nikomatsakis

@arielb1
Copy link
Contributor

arielb1 commented Jul 17, 2017

Also for when you are dealing with the same struct

struct Ref<'a, 'b> { a: &'a u32, b: &'b u32 }

fn foo(mut x: Ref) {
              ---
              1st lifetime parameter on `Ref` must match
              2nd lifetime parameter on `Ref` must match
  x.a = x.b;
}

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions labels Jul 19, 2017
@gaurikholkar-zz
Copy link
Author

gaurikholkar-zz commented Jul 19, 2017

working on this.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@gaurikholkar-zz
Copy link
Author

Making a generalized message as follows

  | fn foo(mut x: Vec<Ref<T>>,
  |                  --- 
  |       y: Ref<T>) 
  |          --- these two structs are not declared with the same lifetime ...
  |{
  | x.push(y);
  |        ^ ... but data flows from `y` into `x` here
  | }

and

struct Ref<'a> { x: &'a u32 }

fn foo(mut x: Vec<Ref>, y: Ref) {
                  ---      --- the reference and struct are not declared with the same lifetime ...
  x.push(y);
|            ^ data from `y` flows into `x` here

}

@gaurikholkar-zz
Copy link
Author

Further modifying it as follows


  | fn foo(mut x: Vec<Ref<T>>,
  |                  --- 
  |       y: Ref<T>) 
  |          --- these two structs are not declared with the same lifetime ...
  |{
  | x.push(y);
  |        ^ ... but data flows from `y` into `x` here
  | }
help: consider changing the signature to:
  | fn foo<'a>(mut x: Vec<'a, Ref<T>>, y: Ref<'a, T>)

@gaurikholkar-zz
Copy link
Author

The error message based on the current PR is

+error[E0623]: lifetime mismatch
+  --> $DIR/ex3-both-anon-regions-both-are-structs.rs:15:12
+   |
+14 | fn foo(mut x: Vec<Ref>, y: Ref) {
+   |                   ---      --- these structs are declared with different lifetimes...
+15 |     x.push(y);
+   |            ^ ...but data from `y` flows into `x` here
+
+error: aborting due to previous error

@gaurikholkar-zz
Copy link
Author

+error[E0623]: lifetime mismatch
+  --> $DIR/ex3-both-anon-regions-both-are-structs.rs:15:12
+   |
+14 | fn foo(mut x: Vec<Ref>, y: Ref) {
+   |                   ---      --- these two types are declared with different lifetimes...
+15 |     x.push(y);
+   |            ^ ...but data from `y` flows into `x` here
+
+error: aborting due to previous error

bors added a commit that referenced this issue Aug 25, 2017
Adding E0623 for structs

This is a fix to #43275

The error message is
```
+error[E0623]: lifetime mismatch
+  --> $DIR/ex3-both-anon-regions-both-are-structs.rs:15:12
+   |
+14 | fn foo(mut x: Vec<Ref>, y: Ref) {
+   |                   ---      --- these structs are declared with different lifetimes...
+15 |     x.push(y);
+   |            ^ ...but data from `y` flows into `x` here
+
+error: aborting due to previous error
```

r? @nikomatsakis
@nikomatsakis
Copy link
Contributor

Seems like this is basically done. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants