Skip to content

Commit b60d732

Browse files
committed
rustc_hir: nix rustc_errors dep
1 parent f07802c commit b60d732

File tree

9 files changed

+40
-42
lines changed

9 files changed

+40
-42
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3730,7 +3730,6 @@ dependencies = [
37303730
"rustc_ast",
37313731
"rustc_ast_pretty",
37323732
"rustc_data_structures",
3733-
"rustc_errors",
37343733
"rustc_index",
37353734
"rustc_macros",
37363735
"rustc_span",

src/librustc_hir/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ rustc_macros = { path = "../librustc_macros" }
1616
rustc_data_structures = { path = "../librustc_data_structures" }
1717
rustc_index = { path = "../librustc_index" }
1818
rustc_span = { path = "../librustc_span" }
19-
rustc_errors = { path = "../librustc_errors" }
2019
rustc_serialize = { path = "../libserialize", package = "serialize" }
2120
rustc_ast = { path = "../librustc_ast" }
2221
lazy_static = "1"

src/librustc_hir/hir.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_ast::node_id::NodeMap;
1111
use rustc_ast::util::parser::ExprPrecedence;
1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
14-
use rustc_errors::FatalError;
1514
use rustc_macros::HashStable_Generic;
1615
use rustc_span::source_map::{SourceMap, Spanned};
1716
use rustc_span::symbol::{kw, sym, Symbol};
@@ -366,9 +365,9 @@ pub enum GenericBound<'hir> {
366365
}
367366

368367
impl GenericBound<'_> {
369-
pub fn trait_def_id(&self) -> Option<DefId> {
368+
pub fn trait_ref(&self) -> Option<&TraitRef<'_>> {
370369
match self {
371-
GenericBound::Trait(data, _) => Some(data.trait_ref.trait_def_id()),
370+
GenericBound::Trait(data, _) => Some(&data.trait_ref),
372371
_ => None,
373372
}
374373
}
@@ -2204,13 +2203,10 @@ pub struct TraitRef<'hir> {
22042203

22052204
impl TraitRef<'_> {
22062205
/// Gets the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
2207-
pub fn trait_def_id(&self) -> DefId {
2206+
pub fn trait_def_id(&self) -> Option<DefId> {
22082207
match self.path.res {
2209-
Res::Def(DefKind::Trait, did) => did,
2210-
Res::Def(DefKind::TraitAlias, did) => did,
2211-
Res::Err => {
2212-
FatalError.raise();
2213-
}
2208+
Res::Def(DefKind::Trait | DefKind::TraitAlias, did) => Some(did),
2209+
Res::Err => None,
22142210
_ => unreachable!(),
22152211
}
22162212
}

src/librustc_hir/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(const_fn)] // For the unsizing cast on `&[]`
88
#![feature(const_panic)]
99
#![feature(in_band_lifetimes)]
10+
#![feature(or_patterns)]
1011
#![feature(specialization)]
1112
#![recursion_limit = "256"]
1213

src/librustc_trait_selection/traits/error_reporting/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15821582
for param in generics.params {
15831583
if param.span == *span
15841584
&& !param.bounds.iter().any(|bound| {
1585-
bound.trait_def_id() == self.tcx.lang_items().sized_trait()
1585+
bound.trait_ref().and_then(|trait_ref| trait_ref.trait_def_id())
1586+
== self.tcx.lang_items().sized_trait()
15861587
})
15871588
{
15881589
let (span, separator) = match param.bounds {

src/librustc_trait_selection/traits/object_safety.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::traits::query::evaluate_obligation::InferCtxtExt;
1515
use crate::traits::{self, Obligation, ObligationCause};
1616
use rustc::ty::subst::{InternalSubsts, Subst};
1717
use rustc::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
18-
use rustc_errors::Applicability;
18+
use rustc_errors::{Applicability, FatalError};
1919
use rustc_hir as hir;
2020
use rustc_hir::def_id::DefId;
2121
use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY;
@@ -170,6 +170,24 @@ fn object_safety_violations_for_trait(
170170
violations
171171
}
172172

173+
fn trait_bound_spans<'tcx>(
174+
tcx: TyCtxt<'tcx>,
175+
bounds: hir::GenericBounds<'tcx>,
176+
) -> impl 'tcx + Iterator<Item = Span> {
177+
bounds.iter().filter_map(move |b| match b {
178+
hir::GenericBound::Trait(trait_ref, hir::TraitBoundModifier::None)
179+
if trait_has_sized_self(
180+
tcx,
181+
trait_ref.trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
182+
) =>
183+
{
184+
// Fetch spans for supertraits that are `Sized`: `trait T: Super`
185+
Some(trait_ref.span)
186+
}
187+
_ => None,
188+
})
189+
}
190+
173191
fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> {
174192
tcx.hir()
175193
.get_if_local(trait_def_id)
@@ -189,33 +207,14 @@ fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]>
189207
{
190208
// Fetch spans for trait bounds that are Sized:
191209
// `trait T where Self: Pred`
192-
Some(pred.bounds.iter().filter_map(|b| match b {
193-
hir::GenericBound::Trait(
194-
trait_ref,
195-
hir::TraitBoundModifier::None,
196-
) if trait_has_sized_self(
197-
tcx,
198-
trait_ref.trait_ref.trait_def_id(),
199-
) =>
200-
{
201-
Some(trait_ref.span)
202-
}
203-
_ => None,
204-
}))
210+
Some(trait_bound_spans(tcx, pred.bounds))
205211
}
206212
_ => None,
207213
}
208214
})
209215
.flatten()
210-
.chain(bounds.iter().filter_map(|b| match b {
211-
hir::GenericBound::Trait(trait_ref, hir::TraitBoundModifier::None)
212-
if trait_has_sized_self(tcx, trait_ref.trait_ref.trait_def_id()) =>
213-
{
214-
// Fetch spans for supertraits that are `Sized`: `trait T: Super`
215-
Some(trait_ref.span)
216-
}
217-
_ => None,
218-
}))
216+
// Fetch spans for supertraits that are `Sized`: `trait T: Super`.
217+
.chain(trait_bound_spans(tcx, bounds))
219218
.collect::<SmallVec<[Span; 1]>>(),
220219
),
221220
_ => None,

src/librustc_typeck/astconv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc::ty::{GenericParamDef, GenericParamDefKind};
1616
use rustc_ast::ast;
1717
use rustc_ast::util::lev_distance::find_best_match_for_name;
1818
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
19-
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId};
19+
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, FatalError};
2020
use rustc_hir as hir;
2121
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
2222
use rustc_hir::def_id::DefId;
@@ -991,7 +991,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
991991

992992
self.ast_path_to_mono_trait_ref(
993993
trait_ref.path.span,
994-
trait_ref.trait_def_id(),
994+
trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
995995
self_ty,
996996
trait_ref.path.segments.last().unwrap(),
997997
)
@@ -1007,7 +1007,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10071007
bounds: &mut Bounds<'tcx>,
10081008
speculative: bool,
10091009
) -> Result<(), GenericArgCountMismatch> {
1010-
let trait_def_id = trait_ref.trait_def_id();
1010+
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
10111011

10121012
debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id);
10131013

src/librustc_typeck/check/coercion.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,12 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14021402
{
14031403
// Are of this `impl Trait`'s traits object safe?
14041404
is_object_safe = bounds.iter().all(|bound| {
1405-
bound.trait_def_id().map_or(false, |def_id| {
1406-
fcx.tcx.object_safety_violations(def_id).is_empty()
1407-
})
1405+
bound
1406+
.trait_ref()
1407+
.and_then(|t| t.trait_def_id())
1408+
.map_or(false, |def_id| {
1409+
fcx.tcx.object_safety_violations(def_id).is_empty()
1410+
})
14081411
})
14091412
}
14101413
}

src/librustc_typeck/check/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10571057
let trait_def_ids: FxHashSet<DefId> = param
10581058
.bounds
10591059
.iter()
1060-
.filter_map(|bound| bound.trait_def_id())
1060+
.filter_map(|bound| Some(bound.trait_ref()?.trait_def_id()?))
10611061
.collect();
10621062
if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
10631063
err.span_suggestions(

0 commit comments

Comments
 (0)