Skip to content

Commit e205784

Browse files
committed
Don't try to resolve type-dependent paths in anon consts
1 parent efaf441 commit e205784

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/librustdoc/html/render/span_map.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
247247
self.maybe_typeck_results = maybe_typeck_results;
248248
}
249249

250+
fn visit_anon_const(&mut self, ct: &'tcx hir::AnonConst) {
251+
// FIXME: Typeck'ing anon consts leads to ICEs in rustc if the parent body wasn't typeck'ed
252+
// yet. See #156418. Figure out what the best and proper solution for this is. Until
253+
// then, let's prevent `typeck` from being called on anon consts by not setting
254+
// `maybe_typeck_results` to `Some(_)`.
255+
let maybe_typeck_results = self.maybe_typeck_results.take();
256+
self.visit_body(self.tcx.hir_body(ct.body));
257+
self.maybe_typeck_results = maybe_typeck_results;
258+
}
259+
250260
fn visit_path(&mut self, path: &hir::Path<'tcx>, _id: HirId) {
251261
if self.handle_macro(path.span) {
252262
return;
@@ -318,9 +328,9 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
318328
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
319329
match expr.kind {
320330
ExprKind::MethodCall(segment, ..) => {
321-
// Exprs *have* to exist in a body, so typeck results should always be available.
322-
let typeck_results = self.maybe_typeck_results().unwrap();
323-
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
331+
if let Some(typeck_results) = self.maybe_typeck_results()
332+
&& let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id)
333+
{
324334
self.matches.insert(segment.ident.span.into(), self.link_for_def(def_id));
325335
}
326336
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Ensure that we don't crash on anonymous constants.
2+
// FIXME: Ideally, we would actually support linkifying type-dependent paths in
3+
// anon consts but for now that's disabled until we figure out what the
4+
// proper solution is implementation-wise.
5+
// issue: <https://github.com/rust-lang/rust/issues/156418>
6+
//@ check-pass
7+
8+
fn scope() {
9+
struct Hold<const N: usize>;
10+
let _ = X::<{ 0usize.saturating_add(1) }>;
11+
}

0 commit comments

Comments
 (0)