Struct compiler_base_session::Session
source · [−]pub struct Session {
pub sm: Arc<SourceMap>,
pub diag_handler: Arc<DiagnosticHandler>,
}
Expand description
Represents the data associated with a compilation session for a single crate.
Note: TODO(zongz): This is a WIP structure. Currently only contains the part related to error diagnostic displaying.
Fields
sm: Arc<SourceMap>
diag_handler: Arc<DiagnosticHandler>
Implementations
sourceimpl Session
impl Session
sourcepub fn new(sm: Arc<SourceMap>, diag_handler: Arc<DiagnosticHandler>) -> Self
pub fn new(sm: Arc<SourceMap>, diag_handler: Arc<DiagnosticHandler>) -> Self
Construct a Session
Examples
// 1. You should create a new `SourceMap` wrapped with `Arc`.
let filename = fs::canonicalize(&PathBuf::from("./src/test_datas/code_snippet")).unwrap().display().to_string();
let src = std::fs::read_to_string(filename.clone()).unwrap();
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
sm.new_source_file(PathBuf::from(filename.clone()).into(), src.to_string());
// 2. You should create a new `DiagnosticHandler` wrapped with `Arc`.
let diag_handler = Arc::new(DiagnosticHandler::new_with_template_dir("./src/test_datas/locales/en-US").unwrap());
// 3. Create `Session`
let sess = Session::new(sm, diag_handler);
sourcepub fn new_with_file_and_code(filename: &str, code: Option<&str>) -> Result<Self>
pub fn new_with_file_and_code(filename: &str, code: Option<&str>) -> Result<Self>
Construct a Session
with file name and optional source code.
In the method, a SourceMap
with a SourceFile
will be created from filename
and the optional source code code
.
Note: code
has higher priority than filename
,
If code
is not None and the content in file filename
is not the same as code
,
then the content in code
will be used as the source code.
If code
is None, the session will use the content of file filename
as source code.
Examples
const CARGO_ROOT: &str = env!("CARGO_MANIFEST_DIR");
let mut cargo_file_path = PathBuf::from(CARGO_ROOT);
cargo_file_path.push("src/test_datas/code_snippet");
let abs_path = cargo_file_path.to_str().unwrap();
let sess = Session::new_with_file_and_code(abs_path, None);
The sess
will take the content of file abs_path
as source code.
const CARGO_ROOT: &str = env!("CARGO_MANIFEST_DIR");
let mut cargo_file_path = PathBuf::from(CARGO_ROOT);
cargo_file_path.push("src/test_datas/code_snippet");
let abs_path = cargo_file_path.to_str().unwrap();
let sess = Session::new_with_file_and_code(abs_path, Some("This is tmp source code"));
The sess
will take “This is tmp source code” as source code.
sourcepub fn new_with_src_code(code: &str) -> Result<Self>
pub fn new_with_src_code(code: &str) -> Result<Self>
Construct a Session
with source code.
In the method, a SourceMap
with a SourceFile
will be created from an empty path.
Examples
let sess = Session::new_with_src_code("This is the source code");
sourcepub fn emit_err(&self, err: impl SessionDiagnostic) -> Result<bool>
pub fn emit_err(&self, err: impl SessionDiagnostic) -> Result<bool>
Emit error diagnostic to terminal.
Panics
After emitting the error diagnositc, the program will panic.
Examples
If you want to emit an error diagnostic.
// 1. Create your own error type.
struct MyError;
// 2. Implement trait `SessionDiagnostic` manually.
impl SessionDiagnostic for MyError {
fn into_diagnostic(self, sess: &Session) -> Result<Diagnostic<DiagnosticStyle>> {
let mut diag = Diagnostic::<DiagnosticStyle>::new();
// 1. Label Component
let label_component = Box::new(Label::Error("error".to_string()));
diag.append_component(label_component);
Ok(diag)
}
}
let result = std::panic::catch_unwind(|| {
// 3. Create a Session.
let sess = Session::new_with_src_code("test code").unwrap();
// 4. Emit the error diagnostic.
sess.emit_err(MyError {}).unwrap();
});
assert!(result.is_err());