Skip to content

Commit 5265919

Browse files
committed
Move ending_semi_or_hi to SourceMap for code reuse.
1 parent 57859ba commit 5265919

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
216216
if body.is_none() && !is_instrinsic {
217217
self.dcx().emit_err(FnWithoutBody {
218218
span,
219-
replace_span: {
220-
let source_map = self.tcx.sess.source_map();
221-
let end = source_map.end_point(span);
222-
if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
223-
end
224-
} else {
225-
span.shrink_to_hi()
226-
}
227-
},
219+
replace_span: self.tcx.sess.source_map().span_ending_semi_or_hi(span),
228220
extern_block_suggestion: match header.ext {
229221
Extern::None => None,
230222
Extern::Implicit(start_span) => Some(ExternBlockSuggestion::Implicit {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,6 @@ impl<'a> AstValidator<'a> {
398398
}
399399
}
400400

401-
/// If `sp` ends with a semicolon, returns it as a `Span`
402-
/// Otherwise, returns `sp.shrink_to_hi()`
403-
fn ending_semi_or_hi(&self, sp: Span) -> Span {
404-
let source_map = self.sess.source_map();
405-
let end = source_map.end_point(sp);
406-
407-
if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
408-
end
409-
} else {
410-
sp.shrink_to_hi()
411-
}
412-
}
413-
414401
fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) {
415402
let span = match bounds {
416403
[] => return,
@@ -1053,7 +1040,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10531040
if expr.is_none() {
10541041
self.dcx().emit_err(errors::ConstWithoutBody {
10551042
span: item.span,
1056-
replace_span: self.ending_semi_or_hi(item.span),
1043+
replace_span: self.sess.source_map().span_ending_semi_or_hi(item.span),
10571044
});
10581045
}
10591046
}
@@ -1066,7 +1053,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10661053
if expr.is_none() {
10671054
self.dcx().emit_err(errors::StaticWithoutBody {
10681055
span: item.span,
1069-
replace_span: self.ending_semi_or_hi(item.span),
1056+
replace_span: self.sess.source_map().span_ending_semi_or_hi(item.span),
10701057
});
10711058
}
10721059
}
@@ -1077,7 +1064,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10771064
if ty.is_none() {
10781065
self.dcx().emit_err(errors::TyAliasWithoutBody {
10791066
span: item.span,
1080-
replace_span: self.ending_semi_or_hi(item.span),
1067+
replace_span: self.sess.source_map().span_ending_semi_or_hi(item.span),
10811068
});
10821069
}
10831070
self.check_type_no_bounds(bounds, "this context");
@@ -1398,22 +1385,22 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13981385
AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
13991386
self.dcx().emit_err(errors::AssocConstWithoutBody {
14001387
span: item.span,
1401-
replace_span: self.ending_semi_or_hi(item.span),
1388+
replace_span: self.sess.source_map().span_ending_semi_or_hi(item.span),
14021389
});
14031390
}
14041391
AssocItemKind::Fn(box Fn { body, .. }) => {
14051392
if body.is_none() {
14061393
self.dcx().emit_err(errors::AssocFnWithoutBody {
14071394
span: item.span,
1408-
replace_span: self.ending_semi_or_hi(item.span),
1395+
replace_span: self.sess.source_map().span_ending_semi_or_hi(item.span),
14091396
});
14101397
}
14111398
}
14121399
AssocItemKind::Type(box TyAlias { bounds, ty, .. }) => {
14131400
if ty.is_none() {
14141401
self.dcx().emit_err(errors::AssocTypeWithoutBody {
14151402
span: item.span,
1416-
replace_span: self.ending_semi_or_hi(item.span),
1403+
replace_span: self.sess.source_map().span_ending_semi_or_hi(item.span),
14171404
});
14181405
}
14191406
self.check_type_no_bounds(bounds, "`impl`s");

compiler/rustc_span/src/source_map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,13 @@ impl SourceMap {
10801080
let span = self.next_point(span);
10811081
if self.span_to_snippet(span).as_deref() == Ok(";") { Some(span) } else { None }
10821082
}
1083+
1084+
/// If `sp` ends with a semicolon, returns it as a `Span`
1085+
/// Otherwise, returns `sp.shrink_to_hi()`
1086+
pub fn span_ending_semi_or_hi(&self, sp: Span) -> Span {
1087+
let end = self.end_point(sp);
1088+
if self.span_to_snippet(end).is_ok_and(|s| s == ";") { end } else { sp.shrink_to_hi() }
1089+
}
10831090
}
10841091

10851092
pub fn get_source_map() -> Option<Arc<SourceMap>> {

0 commit comments

Comments
 (0)