Skip to content

Commit 24117f3

Browse files
committed
rustdoc: fix inserting source code spans for constant values
This will go wrong when the constants partially result from macro expansion. Instead, use the expressions and pretty-print them as Rust code. Fixes: #33302
1 parent 43c5fef commit 24117f3

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/librustdoc/clean/inline.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc::hir;
2020
use rustc::middle::cstore::{self, CrateStore};
2121
use rustc::hir::def::Def;
2222
use rustc::hir::def_id::DefId;
23+
use rustc::hir::print as pprust;
2324
use rustc::ty::{self, TyCtxt};
2425
use rustc::ty::subst;
2526
use rustc::middle::stability;
@@ -30,7 +31,7 @@ use core::{DocContext, DocAccessLevels};
3031
use doctree;
3132
use clean::{self, GetDefId};
3233

33-
use super::{Clean, ToSource};
34+
use super::Clean;
3435

3536
/// Attempt to inline the definition of a local node id into this AST.
3637
///
@@ -333,8 +334,8 @@ pub fn build_impl(cx: &DocContext,
333334
let did = assoc_const.def_id;
334335
let type_scheme = tcx.lookup_item_type(did);
335336
let default = if assoc_const.has_value {
336-
Some(lookup_const_by_id(tcx, did, None)
337-
.unwrap().0.span.to_src(cx))
337+
Some(pprust::expr_to_string(
338+
lookup_const_by_id(tcx, did, None).unwrap().0))
338339
} else {
339340
None
340341
};
@@ -479,8 +480,6 @@ fn build_module(cx: &DocContext, tcx: &TyCtxt,
479480

480481
fn build_const(cx: &DocContext, tcx: &TyCtxt,
481482
did: DefId) -> clean::Constant {
482-
use rustc::hir::print as pprust;
483-
484483
let (expr, ty) = lookup_const_by_id(tcx, did, None).unwrap_or_else(|| {
485484
panic!("expected lookup_const_by_id to succeed for {:?}", did);
486485
});

src/librustdoc/clean/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use rustc::middle::cstore::{self, CrateStore};
3939
use rustc::middle::privacy::AccessLevels;
4040
use rustc::hir::def::Def;
4141
use rustc::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
42+
use rustc::hir::print as pprust;
4243
use rustc::ty::subst::{self, ParamSpace, VecPerParamSpace};
4344
use rustc::ty;
4445
use rustc::middle::stability;
@@ -1285,8 +1286,7 @@ impl Clean<Item> for hir::TraitItem {
12851286
let inner = match self.node {
12861287
hir::ConstTraitItem(ref ty, ref default) => {
12871288
AssociatedConstItem(ty.clean(cx),
1288-
default.as_ref().map(|expr|
1289-
expr.span.to_src(cx)))
1289+
default.as_ref().map(|e| pprust::expr_to_string(&e)))
12901290
}
12911291
hir::MethodTraitItem(ref sig, Some(_)) => {
12921292
MethodItem(sig.clean(cx))
@@ -1316,7 +1316,7 @@ impl Clean<Item> for hir::ImplItem {
13161316
let inner = match self.node {
13171317
hir::ImplItemKind::Const(ref ty, ref expr) => {
13181318
AssociatedConstItem(ty.clean(cx),
1319-
Some(expr.span.to_src(cx)))
1319+
Some(pprust::expr_to_string(expr)))
13201320
}
13211321
hir::ImplItemKind::Method(ref sig, _) => {
13221322
MethodItem(sig.clean(cx))
@@ -1635,8 +1635,8 @@ impl Clean<Type> for hir::Ty {
16351635
BorrowedRef {lifetime: l.clean(cx), mutability: m.mutbl.clean(cx),
16361636
type_: box m.ty.clean(cx)},
16371637
TyVec(ref ty) => Vector(box ty.clean(cx)),
1638-
TyFixedLengthVec(ref ty, ref e) => FixedVector(box ty.clean(cx),
1639-
e.span.to_src(cx)),
1638+
TyFixedLengthVec(ref ty, ref e) =>
1639+
FixedVector(box ty.clean(cx), pprust::expr_to_string(e)),
16401640
TyTup(ref tys) => Tuple(tys.clean(cx)),
16411641
TyPath(None, ref p) => {
16421642
resolve_type(cx, p.clean(cx), self.id)
@@ -2185,7 +2185,7 @@ impl Clean<Item> for doctree::Static {
21852185
inner: StaticItem(Static {
21862186
type_: self.type_.clean(cx),
21872187
mutability: self.mutability.clean(cx),
2188-
expr: self.expr.span.to_src(cx),
2188+
expr: pprust::expr_to_string(&self.expr),
21892189
}),
21902190
}
21912191
}
@@ -2209,7 +2209,7 @@ impl Clean<Item> for doctree::Constant {
22092209
deprecation: self.depr.clean(cx),
22102210
inner: ConstantItem(Constant {
22112211
type_: self.type_.clean(cx),
2212-
expr: self.expr.span.to_src(cx),
2212+
expr: pprust::expr_to_string(&self.expr),
22132213
}),
22142214
}
22152215
}

src/test/rustdoc/issue-33302.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Ensure constant and array length values are not taken from source
12+
// code, which wreaks havoc with macros.
13+
14+
#![feature(associated_consts)]
15+
16+
macro_rules! make {
17+
($n:expr) => {
18+
pub struct S;
19+
20+
// @has issue_33302/constant.CST.html \
21+
// '//pre[@class="rust const"]' 'pub const CST: i32 = 4 * 4'
22+
pub const CST: i32 = ($n * $n);
23+
// @has issue_33302/static.ST.html \
24+
// '//pre[@class="rust static"]' 'pub static ST: i32 = 4 * 4'
25+
pub static ST: i32 = ($n * $n);
26+
27+
pub trait T<X> {
28+
fn ignore(_: &X) {}
29+
const C: X;
30+
// @has issue_33302/trait.T.html \
31+
// '//*[@class="rust trait"]' 'const D: i32 = 4 * 4;'
32+
// @has - '//*[@id="associatedconstant.D"]' 'const D: i32 = 4 * 4'
33+
const D: i32 = ($n * $n);
34+
}
35+
36+
// @has issue_33302/struct.S.html \
37+
// '//h3[@class="impl"]' 'impl T<[i32; 4 * 4]> for S'
38+
// @has - '//*[@id="associatedconstant.C"]' 'const C: [i32; 4 * 4] = [0; 4 * 4]'
39+
// @has - '//*[@id="associatedconstant.D"]' 'const D: i32 = 4 * 4'
40+
impl T<[i32; ($n * $n)]> for S {
41+
const C: [i32; ($n * $n)] = [0; ($n * $n)];
42+
}
43+
}
44+
}
45+
46+
make!(4);

0 commit comments

Comments
 (0)