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

MIR inlining breaks Location::caller() #105538

Closed
saethlin opened this issue Dec 10, 2022 · 4 comments · Fixed by #106139 or #109307
Closed

MIR inlining breaks Location::caller() #105538

saethlin opened this issue Dec 10, 2022 · 4 comments · Fixed by #106139 or #109307
Assignees
Labels
A-mir-opt Area: MIR optimizations A-mir-opt-inlining Area: MIR inlining C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

use core::panic::Location;

#[inline]
fn nested() -> &'static Location<'static> {
    Location::caller()
}

fn main() {
    println!("{:?}", nested());
}

In debug mode, this prints

Location { file: "src/main.rs", line: 5, col: 5 }

But with optimizations, this prints:

Location { file: "src/main.rs", line: 9, col: 22 }

With optimizations and -Zinline-mir=no, this prints:

Location { file: "src/main.rs", line: 5, col: 5 }

This check in the MIR inliner is hiding this problem from the track-caller UI tests:

// Only inline local functions if they would be eligible for cross-crate
// inlining. This is to ensure that the final crate doesn't have MIR that
// reference unexported symbols
if callsite.callee.def_id().is_local() {
let is_generic = callsite.callee.substs.non_erasable_generics().next().is_some();
if !is_generic && !callee_attrs.requests_inline() {
return Err("not exported");
}
}

@rustbot label +A-mir-opt +A-mir-opt-inlining


rustc --version --verbose:

rustc 1.68.0-nightly (dfe3fe710 2022-12-09)
binary: rustc
commit-hash: dfe3fe710181738a2cb3060c23ec5efb3c68ca09
commit-date: 2022-12-09
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.0.6
@saethlin saethlin added the C-bug Category: This is a bug. label Dec 10, 2022
@rustbot rustbot added A-mir-opt Area: MIR optimizations A-mir-opt-inlining Area: MIR inlining labels Dec 10, 2022
@Noratrieb
Copy link
Member

Noratrieb commented Dec 10, 2022

This isn't specific to Location::caller but to any #[track_caller] function called in an inlined not-track-caller function.

#[track_caller]
fn another() {
    panic!("line 3, right?")
}

#[inline]
fn nested() {
    another();
}

fn main() {
    nested();
}

panics on line 8: thread 'main' panicked at 'line 3, right?', uwu.rs:8:5
Nevermind that, the panic here is correct actually :D

@tmiasko tmiasko added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. labels Dec 14, 2022
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Dec 14, 2022
@tmiasko
Copy link
Contributor

tmiasko commented Dec 14, 2022

Ignoring changes in the standard library and optimization level at which MIR inliner is enabled, bisection points to #94427 cc @cjgillot.

@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Dec 21, 2022
@bors bors closed this as completed in 11a338a Dec 29, 2022
@tmiasko
Copy link
Contributor

tmiasko commented Dec 30, 2022

use std::panic::Location;

macro_rules! f {
    () => { Location::caller() }
}

#[inline(always)]
fn g() -> &'static Location<'static> {
    f!()
}

fn main() {
    println!("{:?}", g());
}
$ rustc a.rs && ./a
Location { file: "a.rs", line: 9, col: 5 }
$ rustc a.rs -O && ./a
Location { file: "a.rs", line: 4, col: 13 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations A-mir-opt-inlining Area: MIR inlining C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants