Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that spans point to char boundaries #106192

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ pub fn set_session_globals_then<R>(session_globals: &SessionGlobals, f: impl FnO
SESSION_GLOBALS.set(session_globals, f)
}

pub fn create_session_if_not_set_then<R, F>(edition: Edition, f: F) -> R
where
F: FnOnce(&SessionGlobals) -> R,
{
pub fn create_session_if_not_set_then<R>(
edition: Edition,
f: impl FnOnce(&SessionGlobals) -> R,
) -> R {
if !SESSION_GLOBALS.is_set() {
let session_globals = SessionGlobals::new(edition);
SESSION_GLOBALS.set(&session_globals, || SESSION_GLOBALS.with(f))
Expand All @@ -160,7 +160,14 @@ where
{
SESSION_GLOBALS.with(f)
}
pub struct NotSet;

#[inline]
pub fn try_with_session_globals<R>(f: impl FnOnce(&SessionGlobals) -> R) -> Result<R, NotSet> {
if SESSION_GLOBALS.is_set() { Ok(SESSION_GLOBALS.with(f)) } else { Err(NotSet) }
}

#[inline]
pub fn create_default_session_globals_then<R>(f: impl FnOnce() -> R) -> R {
create_session_globals_then(edition::DEFAULT_EDITION, f)
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,12 @@ impl SourceMap {
}

/// Returns a new span representing just the first character of the given span.
/// When the span is empty, the same empty span is returned.
pub fn start_point(&self, sp: Span) -> Span {
if sp.is_empty() {
return sp;
}

let width = {
let sp = sp.data();
let local_begin = self.lookup_byte_offset(sp.lo);
Expand Down
36 changes: 35 additions & 1 deletion compiler/rustc_span/src/span_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::def_id::{DefIndex, LocalDefId};
use crate::hygiene::SyntaxContext;
use crate::SPAN_TRACK;
use crate::{try_with_session_globals, NotSet, Pos, SPAN_TRACK};
use crate::{BytePos, SpanData};

use rustc_data_structures::fx::FxIndexSet;
Expand Down Expand Up @@ -103,6 +103,40 @@ impl Span {
ctxt: SyntaxContext,
parent: Option<LocalDefId>,
) -> Self {
// Make sure that the byte positions are at char boundaries.
// Only do this if the session globals are set correctly, which they aren't in some unit tests.
if cfg!(debug_assertions) {
let _: Result<(), NotSet> = try_with_session_globals(|sess| {
let sm = sess.source_map.lock();
if let Some(sm) = &*sm {
let offset = sm.lookup_byte_offset(lo);
if let Some(file) = &offset.sf.src {
// `is_char_boundary` already checks this, but asserting it seperately gives a better panic message.
assert!(
file.len() >= offset.pos.to_usize(),
"start of span is out of bounds"
);
assert!(
file.is_char_boundary(offset.pos.to_usize()),
"start of span not on char boundary"
);
}

let offset = sm.lookup_byte_offset(hi);
if let Some(file) = &offset.sf.src {
assert!(
file.len() >= offset.pos.to_usize(),
"end of span is out of bounds"
);
assert!(
file.is_char_boundary(offset.pos.to_usize()),
"end of span not on char boundary"
);
}
}
});
}

if lo > hi {
std::mem::swap(&mut lo, &mut hi);
}
Expand Down
Loading