Skip to content

Commit

Permalink
Small cleanups
Browse files Browse the repository at this point in the history
- Use a tuple struct instead of a single field
- Enforce calling `source_callsite()` by making the inner span private
- Rename `empty` to `dummy`
  • Loading branch information
jyn514 committed Dec 12, 2020
1 parent 0e574fb commit 9684557
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
};

Some(Item {
source: Span::empty(),
source: Span::dummy(),
name: None,
attrs: Default::default(),
visibility: Inherited,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
items.push(clean::Item {
name: None,
attrs: clean::Attributes::default(),
source: clean::Span::empty(),
source: clean::Span::dummy(),
def_id: DefId::local(CRATE_DEF_INDEX),
visibility: clean::Public,
stability: None,
Expand Down
5 changes: 1 addition & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,10 +1882,7 @@ impl Clean<VariantKind> for hir::VariantData<'_> {

impl Clean<Span> for rustc_span::Span {
fn clean(&self, _cx: &DocContext<'_>) -> Span {
// Get the macro invocation instead of the definition,
// in case the span is result of a macro expansion.
// (See rust-lang/rust#39726)
Span { original: self.source_callsite() }
Span::from_rustc_span(*self)
}
}

Expand Down
25 changes: 15 additions & 10 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,31 +1610,36 @@ crate enum VariantKind {
Struct(VariantStruct),
}

/// Small wrapper around `rustc_span::Span` that adds helper methods.
/// Small wrapper around `rustc_span::Span` that adds helper methods and enforces calling `source_callsite`.
#[derive(Clone, Debug)]
crate struct Span {
crate original: rustc_span::Span,
}
crate struct Span(rustc_span::Span);

impl Span {
crate fn empty() -> Self {
Self { original: rustc_span::DUMMY_SP }
crate fn from_rustc_span(sp: rustc_span::Span) -> Self {
// Get the macro invocation instead of the definition,
// in case the span is result of a macro expansion.
// (See rust-lang/rust#39726)
Self(sp.source_callsite())
}

crate fn dummy() -> Self {
Self(rustc_span::DUMMY_SP)
}

crate fn span(&self) -> rustc_span::Span {
self.original
self.0
}

crate fn filename(&self, sess: &Session) -> FileName {
sess.source_map().span_to_filename(self.original)
sess.source_map().span_to_filename(self.0)
}

crate fn lo(&self, sess: &Session) -> Loc {
sess.source_map().lookup_char_pos(self.original.lo())
sess.source_map().lookup_char_pos(self.0.lo())
}

crate fn hi(&self, sess: &Session) -> Loc {
sess.source_map().lookup_char_pos(self.original.hi())
sess.source_map().lookup_char_pos(self.0.hi())
}

crate fn cnum(&self, sess: &Session) -> CrateNum {
Expand Down

0 comments on commit 9684557

Please sign in to comment.