Lifetimes mismatch

Hi Rustaceans,
my first question is which these two types? Either I'm blind or rustic hiding something from me. If it says two which type is the second?

error[E0623]: lifetime mismatch
  --> src/renderer/src/
48 | fn ray_color(ray: &Ray, world: &World, depth: u32, rng: &mut rand::rngs::SmallRng) -> Color {
   |                                ------
   |                                |
   |                                these two types are declared with different lifetimes...
57 |     if world.hit(ray, 0.001, f32::INFINITY, &mut rec) {
   |              ^^^ ...but data from `world` flows into `world` here

I agree the error message is confusing. How is the World type declared?

Here is the World struct

struct World<'a> {
    pub(crate) objects: Vec<Arc<dyn Hittable<'a>>>

pub trait Hittable<'obj>: Send + Sync {
    fn hit(&'obj self, ray: &Ray, t_min: f32, t_max: f32, rec: &mut HitRecord<'obj>) -> bool;

impl<'obj> Hittable<'obj> for World<'obj> {
let mut world = Arc::new(World::new())
fn render(......, world: Arc<World<'static>) {
    ray_color(..., world: &World)

The problem seems to be in line 57.
world.hit takes &'obj self which is less than 'static

Ok I found the source of issue. I shouldn't put a lifetime on the Hittable and World structs, instead the 'obj lifetime should go to the function definition:

fn hit<'obj>(...)

The original error message is confusing indeed.

Could you come up with a minimized repro in the playground and file a diagnostics ticket in the repo? It'd be nice to make this less confusing.

I wish I could, I went ahead and tried to reproduce, but got a ton of other unrelated errors. :frowning:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.