Skip to content

Latest nightly (rustc 1.64.0-nightly (62b272d25 2022-07-21)) introduces a new borrowcheck error in previously compiling mdbook 0.4.20 #99591

Closed as not planned

Description

@mitchmindtree

As of rustc 1.64.0-nightly (62b272d25 2022-07-21), a new lifetime error has appeared when attempting to build mdbook 0.4.20.

Error

error[E0597]: `local_ctx` does not live long enough
   --> src/renderer/html_handlebars/helpers/navigation.rs:154:25
    |
154 |             t.render(r, &local_ctx, &mut local_rc, out)
    |                         ^^^^^^^^^^ borrowed value does not live long enough
155 |         })?;
    |         -
    |         |
    |         `local_ctx` dropped here while still borrowed
    |         borrow might be used here, when `local_rc` is dropped and runs the destructor for type `handlebars::RenderContext<'_, '_>`
    |
    = note: values in a scope are dropped in the opposite order they are defined

For more information about this error, try `rustc --explain E0597`.
error: could not compile `mdbook` due to previous error

Version it worked on

rustc 1.64.0-nightly (d68e7eb 2022-07-20)

Version with regression

rustc 1.64.0-nightly (62b272d 2022-07-21)

Details

You can find more details at the downstream mdbook issue rust-lang/mdBook#1860.

Curiously, the new error can be fixed by re-ordering some seemingly inocuous statements - see this PR rust-lang/mdBook#1861.

Perhaps this is indicative of an error in non-lexical-lifetime handling on the latest nightly? Or alternatively, this could be the result of fixing a soundness / safety issue related to the interop between lifetimes and drop order?

This error was originally discovered in an unrelated CI pass failing in the following PR: FuelLabs/sway#2359.

Activity

  1. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 22, 2022
  2. ehuss commented on Jul 22, 2022

    @ehuss
    Contributor

    Confirmed that it is caused by #99413. Here is a slightly smaller repro:

    #![allow(unused)]
    use std::collections::BTreeMap;
    use std::marker::PhantomData;
    use std::rc::Rc;
    
    fn main() {
        let mut rc = RenderContext {
            local_helpers: BTreeMap::new(),
        };
        foo(&mut rc);
    }
    
    #[derive(Clone)]
    struct RenderContext<'rc> {
        local_helpers: BTreeMap<String, Rc<dyn HelperDef + 'rc>>,
    }
    
    pub trait HelperDef {}
    
    fn foo(rc: &mut RenderContext<'_>) {
        let mut local_rc = rc.clone();
        let local_ctx: BTreeMap<String, String> = BTreeMap::new();
        render(&local_ctx, &mut local_rc);
    }
    
    fn render<'rc>(ctx: &'rc BTreeMap<String, String>, rc: &mut RenderContext<'rc>) {}

    I think the error here is correct and #99413 is a valid fix. It's just a consequence of how the lifetimes in the handlebars crate are defined that cause the error.

  3. danielhenrymantilla commented on Jul 22, 2022

    @danielhenrymantilla
    Contributor

    Until the fix to mdbook is officially released, btw, for those running into this, the workaround is to use the git dev, potentially as a patch:

    • for those using cargo install mdbook, instead, do

      cargo install mdbook --git 'https://github.com/rust-lang/mdBook' --rev '8f01d0234f708042ea8d0dc5cac63859b3f14cb1'
    • for those depending on mdbook as a library, on the Cargo.toml file of your workspace (where the Cargo.lock file lies / is generated), add:

      [patch.crates-io.mdbook]
      version = "0.4.20"
      git = "https://github.com/rust-lang/mdBook"
      rev = "8f01d0234f708042ea8d0dc5cac63859b3f14cb1"
  4. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 9, 2022
  5. Jules-Bertholet commented on Jun 10, 2023

    @Jules-Bertholet
    Contributor

    @rustbot label -regression-untriaged +regression-from-stable-to-stable

  6. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    and removed
    regression-untriagedUntriaged performance or correctness regression.
    on Jun 10, 2023
  7. apiraino commented on Jun 21, 2023

    @apiraino
    Contributor

    @Jules-Bertholet changed the label to regression on stable: did you experience that it's still an issue? I've compiled mdbook (both on stable and nightly) and the reproducible at this comment now shows a better error, I believe.

  8. ehuss commented on Jun 21, 2023

    @ehuss
    Contributor

    I think this should be closed since it was an intentional soundness fix.

  9. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions