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

fix debuginfo scoping of let-statements #46896

Merged
merged 1 commit into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
name,
source_info,
internal,
lexical_scope,
syntactic_scope,
is_user_variable
});
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref, mutability });
Expand Down
82 changes: 77 additions & 5 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,83 @@ pub struct LocalDecl<'tcx> {
/// Source info of the local.
pub source_info: SourceInfo,

/// The *lexical* visibility scope the local is defined
/// The *syntactic* visibility scope the local is defined
/// in. If the local was defined in a let-statement, this
/// is *within* the let-statement, rather than outside
/// of it.
pub lexical_scope: VisibilityScope,
///
/// This is needed because visibility scope of locals within a let-statement
/// is weird.
///
/// The reason is that we want the local to be *within* the let-statement
/// for lint purposes, but we want the local to be *after* the let-statement
/// for names-in-scope purposes.
///
/// That's it, if we have a let-statement like the one in this
/// function:
/// ```
/// fn foo(x: &str) {
/// #[allow(unused_mut)]
/// let mut x: u32 = { // <- one unused mut
/// let mut y: u32 = x.parse().unwrap();
/// y + 2
/// };
/// drop(x);
/// }
/// ```
///
/// Then, from a lint point of view, the declaration of `x: u32`
/// (and `y: u32`) are within the `#[allow(unused_mut)]` scope - the
/// lint scopes are the same as the AST/HIR nesting.
///
/// However, from a name lookup point of view, the scopes look more like
/// as if the let-statements were `match` expressions:
///
/// ```
/// fn foo(x: &str) {
/// match {
/// match x.parse().unwrap() {
/// y => y + 2
/// }
/// } {
/// x => drop(x)
/// };
/// }
/// ```
///
/// We care about the name-lookup scopes for debuginfo - if the
/// debuginfo instruction pointer is at the call to `x.parse()`, we
/// want `x` to refer to `x: &str`, but if it is at the call to
/// `drop(x)`, we want it to refer to `x: u32`.
///
/// To allow both uses to work, we need to have more than a single scope
/// for a local. We have the `syntactic_scope` represent the
/// "syntactic" lint scope (with a variable being under its let
/// block) while the source-info scope represents the "local variable"
/// scope (where the "rest" of a block is under all prior let-statements).
///
/// The end result looks like this:
///
/// ROOT SCOPE
/// │{ argument x: &str }
/// │
/// │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
/// │ │ // in practice because I'm lazy.
/// │ │
/// │ │← x.syntactic_scope
/// │ │← `x.parse().unwrap()`
/// │ │
/// │ │ │← y.syntactic_scope
/// │ │
/// │ │ │{ let y: u32 }
/// │ │ │
/// │ │ │← y.source_info.scope
/// │ │ │← `y + 2`
/// │
/// │ │{ let x: u32 }
/// │ │← x.source_info.scope
/// │ │← `drop(x)` // this accesses `x: u32`
pub syntactic_scope: VisibilityScope,
}

impl<'tcx> LocalDecl<'tcx> {
Expand All @@ -499,7 +571,7 @@ impl<'tcx> LocalDecl<'tcx> {
span,
scope: ARGUMENT_VISIBILITY_SCOPE
},
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: false,
is_user_variable: false
}
Expand All @@ -516,7 +588,7 @@ impl<'tcx> LocalDecl<'tcx> {
span,
scope: ARGUMENT_VISIBILITY_SCOPE
},
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: true,
is_user_variable: false
}
Expand All @@ -534,7 +606,7 @@ impl<'tcx> LocalDecl<'tcx> {
span,
scope: ARGUMENT_VISIBILITY_SCOPE
},
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: false,
name: None, // FIXME maybe we do want some name here?
is_user_variable: false
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ macro_rules! make_mir_visitor {
name: _,
ref $($mutability)* source_info,
internal: _,
ref $($mutability)* lexical_scope,
ref $($mutability)* syntactic_scope,
is_user_variable: _,
} = *local_decl;

Expand All @@ -711,7 +711,7 @@ macro_rules! make_mir_visitor {
source_info: *source_info,
});
self.visit_source_info(source_info);
self.visit_visibility_scope(lexical_scope);
self.visit_visibility_scope(syntactic_scope);
}

fn super_visibility_scope(&mut self,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
ty: ptr_ty,
name: None,
source_info,
lexical_scope: source_info.scope,
syntactic_scope: source_info.scope,
internal: true,
is_user_variable: false
});
Expand Down
22 changes: 11 additions & 11 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,27 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
pattern: &Pattern<'tcx>)
-> Option<VisibilityScope> {
assert!(!(var_scope.is_some() && lint_level.is_explicit()),
"can't have both a var and a lint scope at the same time");
"can't have both a var and a lint scope at the same time");
let mut syntactic_scope = self.visibility_scope;
self.visit_bindings(pattern, &mut |this, mutability, name, var, span, ty| {
if var_scope.is_none() {
var_scope = Some(this.new_visibility_scope(scope_span,
LintLevel::Inherited,
None));
// If we have lints, create a new visibility scope
// that marks the lints for the locals.
// that marks the lints for the locals. See the comment
// on the `syntactic_scope` field for why this is needed.
if lint_level.is_explicit() {
this.visibility_scope =
syntactic_scope =
this.new_visibility_scope(scope_span, lint_level, None);
}
}
let source_info = SourceInfo {
span,
scope: var_scope.unwrap()
};
this.declare_binding(source_info, mutability, name, var, ty);
this.declare_binding(source_info, syntactic_scope, mutability, name, var, ty);
});
// Pop any scope we created for the locals.
if let Some(var_scope) = var_scope {
self.visibility_scope = var_scope;
}
var_scope
}

Expand Down Expand Up @@ -783,21 +781,23 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {

fn declare_binding(&mut self,
source_info: SourceInfo,
syntactic_scope: VisibilityScope,
mutability: Mutability,
name: Name,
var_id: NodeId,
var_ty: Ty<'tcx>)
-> Local
{
debug!("declare_binding(var_id={:?}, name={:?}, var_ty={:?}, source_info={:?})",
var_id, name, var_ty, source_info);
debug!("declare_binding(var_id={:?}, name={:?}, var_ty={:?}, source_info={:?}, \
syntactic_scope={:?})",
var_id, name, var_ty, source_info, syntactic_scope);

let var = self.local_decls.push(LocalDecl::<'tcx> {
mutability,
ty: var_ty.clone(),
name: Some(name),
source_info,
lexical_scope: self.visibility_scope,
syntactic_scope,
internal: false,
is_user_variable: true,
});
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
// RustCall pseudo-ABI untuples the last argument.
spread_arg = Some(Local::new(arguments.len()));
}
let closure_expr_id = tcx.hir.local_def_id(fn_id);
info!("fn_id {:?} has attrs {:?}", closure_expr_id,
tcx.get_attrs(closure_expr_id));

// Gather the upvars of a closure, if any.
let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
Expand Down Expand Up @@ -571,7 +574,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
scope: ARGUMENT_VISIBILITY_SCOPE,
span: pattern.map_or(self.fn_span, |pat| pat.span)
},
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
name,
internal: false,
is_user_variable: false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
LocalDecl {
mutability, ty, name: None,
source_info: SourceInfo { scope: ARGUMENT_VISIBILITY_SCOPE, span },
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: false,
is_user_variable: false
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn replace_result_variable<'tcx>(ret_ty: Ty<'tcx>,
ty: ret_ty,
name: None,
source_info: source_info(mir),
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: false,
is_user_variable: false,
};
Expand Down Expand Up @@ -562,7 +562,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
ty: tcx.mk_nil(),
name: None,
source_info,
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: false,
is_user_variable: false,
};
Expand All @@ -578,7 +578,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
}),
name: None,
source_info,
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
internal: false,
is_user_variable: false,
};
Expand Down
32 changes: 32 additions & 0 deletions src/test/debuginfo/shadowed-variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@
// gdb-check:$6 = 20
// gdb-command:continue

// gdb-command:print x
// gdb-check:$5 = 10.5
// gdb-command:print y
// gdb-check:$6 = 20
// gdb-command:continue

// gdb-command:print x
// gdb-check:$5 = 11.5
// gdb-command:print y
// gdb-check:$6 = 20
// gdb-command:continue

// === LLDB TESTS ==================================================================================

Expand All @@ -57,6 +68,18 @@
// lldb-check:[...]$5 = 20
// lldb-command:continue

// lldb-command:print x
// lldb-check:[...]$4 = 10.5
// lldb-command:print y
// lldb-check:[...]$5 = 20
// lldb-command:continue

// lldb-command:print x
// lldb-check:[...]$4 = 11.5
// lldb-command:print y
// lldb-check:[...]$5 = 20
// lldb-command:continue

#![feature(omit_gdb_pretty_printer_section)]
#![omit_gdb_pretty_printer_section]

Expand All @@ -77,6 +100,15 @@ fn main() {

zzz(); // #break
sentinel();

let x = {
zzz(); // #break
sentinel();
11.5
};

zzz(); // #break
sentinel()
}

fn zzz() {()}
Expand Down