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

Use DefId instead of Span in BrAnon to avoid stable hashing collisions #104274

Closed
Closed
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 compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {

fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> {
let v = match def {
Some(def) => infer::EarlyBoundRegion(span, def.name),
Some(def) => infer::EarlyBoundRegion(span, def.def_id),
None => infer::MiscVariable(span),
};
Some(self.next_region_var(v))
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_hir_typeck/src/generator_interior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ pub fn resolve_interior<'a, 'tcx>(
// typeck had previously found constraints that would cause them to be related.

let mut counter = 0;
let mut mk_bound_region = |span| {
let kind = ty::BrAnon(counter, span);
let mut mk_bound_region = |def_id| {
let kind = ty::BrAnon(counter, def_id);
let var = ty::BoundVar::from_u32(counter);
counter += 1;
ty::BoundRegion { var, kind }
Expand All @@ -240,22 +240,18 @@ pub fn resolve_interior<'a, 'tcx>(
ty::ReVar(vid) => {
let origin = fcx.region_var_origin(vid);
match origin {
RegionVariableOrigin::EarlyBoundRegion(span, _) => {
mk_bound_region(Some(span))
RegionVariableOrigin::EarlyBoundRegion(_, def_id) => {
mk_bound_region(Some(def_id))
}
_ => mk_bound_region(None),
}
}
// FIXME: these should use `BrNamed`
ty::ReEarlyBound(region) => {
mk_bound_region(Some(fcx.tcx.def_span(region.def_id)))
}
ty::ReEarlyBound(region) => mk_bound_region(Some(region.def_id)),
ty::ReLateBound(_, ty::BoundRegion { kind, .. })
| ty::ReFree(ty::FreeRegion { bound_region: kind, .. }) => match kind {
ty::BoundRegionKind::BrAnon(_, span) => mk_bound_region(span),
ty::BoundRegionKind::BrNamed(def_id, _) => {
mk_bound_region(Some(fcx.tcx.def_span(def_id)))
}
ty::BoundRegionKind::BrAnon(_, def_id) => mk_bound_region(def_id),
ty::BoundRegionKind::BrNamed(def_id, _) => mk_bound_region(Some(def_id)),
ty::BoundRegionKind::BrEnv => mk_bound_region(None),
},
_ => mk_bound_region(None),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_infer/src/errors/note_and_explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ impl<'a> DescriptionCtx<'a> {
};
me.span = Some(sp);
}
ty::BrAnon(idx, span) => {
ty::BrAnon(idx, def_id) => {
me.kind = "anon_num_here";
me.num_arg = idx+1;
me.span = match span {
Some(_) => span,
me.span = match def_id {
Some(def_id) => Some(tcx.def_span(def_id)),
None => Some(tcx.def_span(scope)),
}
},
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
};
(text, sp)
}
ty::BrAnon(idx, span) => (
ty::BrAnon(idx, def_id) => (
format!("the anonymous lifetime #{} defined here", idx + 1),
match span {
Some(span) => span,
match def_id {
Some(def_id) => tcx.def_span(def_id),
None => tcx.def_span(scope)
}
),
Expand Down Expand Up @@ -3048,7 +3048,9 @@ impl<'tcx> InferCtxt<'tcx> {
br_string(br),
self.tcx.associated_item(def_id).name
),
infer::EarlyBoundRegion(_, name) => format!(" for lifetime parameter `{}`", name),
infer::EarlyBoundRegion(_, def_id) => {
format!(" for lifetime parameter `{}`", self.tcx.item_name(def_id))
}
infer::UpvarRegion(ref upvar_id, _) => {
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
format!(" for capture of `{}` by closure", var_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
ty::BrNamed(def_id, symbol) => {
(Some(self.tcx().def_span(def_id)), Some(symbol))
}
ty::BrAnon(_, span) => (*span, None),
ty::BrAnon(_, Some(def_id)) => (Some(self.tcx().def_span(def_id)), None),
ty::BrAnon(_, None) => (None, None),
ty::BrEnv => (None, None),
};
let (sup_span, sup_symbol) = match sup_name {
ty::BrNamed(def_id, symbol) => {
(Some(self.tcx().def_span(def_id)), Some(symbol))
}
ty::BrAnon(_, span) => (*span, None),
ty::BrAnon(_, Some(def_id)) => (Some(self.tcx().def_span(def_id)), None),
ty::BrAnon(_, None) => (None, None),
ty::BrEnv => (None, None),
};
match (sub_span, sup_span, sub_symbol, sup_symbol) {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use rustc_middle::ty::visit::TypeVisitable;
pub use rustc_middle::ty::IntVarValue;
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};

use std::cell::{Cell, RefCell};
Expand Down Expand Up @@ -470,7 +469,7 @@ pub enum RegionVariableOrigin {
Coercion(Span),

/// Region variables created as the values for early-bound regions
EarlyBoundRegion(Span, Symbol),
EarlyBoundRegion(Span, DefId),

/// Region variables created for bound regions
/// in a function or method that is called
Expand Down Expand Up @@ -1181,7 +1180,7 @@ impl<'tcx> InferCtxt<'tcx> {
GenericParamDefKind::Lifetime => {
// Create a region inference variable for the given
// region parameter definition.
self.next_region_var(EarlyBoundRegion(span, param.name)).into()
self.next_region_var(EarlyBoundRegion(span, param.def_id)).into()
}
GenericParamDefKind::Type { .. } => {
// Create a type inference variable for the given
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct FreeRegion {
#[derive(HashStable)]
pub enum BoundRegionKind {
/// An anonymous region parameter for a given fn (&T)
BrAnon(u32, Option<Span>),
BrAnon(u32, Option<DefId>),

/// Named region parameters for functions (a in &'a T)
///
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/generic-associated-types/bugs/issue-100013.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ LL | | }
| |_____^
|
note: the lifetime defined here...
--> $DIR/issue-100013.rs:17:38
--> $DIR/issue-100013.rs:9:21
|
LL | let x = None::<I::Future<'_, '_>>; // a type referencing GAT
| ^^
LL | type Future<'s, 'cx>: Send
| ^^^
note: ...must outlive the lifetime defined here
--> $DIR/issue-100013.rs:17:34
--> $DIR/issue-100013.rs:9:17
|
LL | let x = None::<I::Future<'_, '_>>; // a type referencing GAT
| ^^
LL | type Future<'s, 'cx>: Send
| ^^
= note: this is a known limitation that will be removed in the future (see issue #100013 <https://github.com/rust-lang/rust/issues/100013> for more information)

error: lifetime bound not satisfied
Expand Down