Skip to content

Commit

Permalink
remove cyclic global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Medowhill committed Jul 21, 2024
1 parent 02c1552 commit 50619d2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
87 changes: 85 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ use rustc_errors::{
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hir::{
def::Res,
def_id::DefId,
def_id::{DefId, LocalDefId},
hir_id::HirId,
intravisit::{self, Visitor},
Expr, ExprKind, FnDecl, FnRetTy, GenericArg, GenericBound, GenericParam, GenericParamKind,
ItemKind, MutTy, Mutability, Path, PathSegment, PrimTy, QPath, TraitRef, Ty, TyKind,
Item, ItemKind, MutTy, Mutability, Node, Path, PathSegment, PrimTy, QPath, TraitRef, Ty,
TyKind,
};
use rustc_interface::{interface::Compiler, Config};
use rustc_middle::{dep_graph::DepContext, hir::nested_filter, ty::TyCtxt};
Expand Down Expand Up @@ -1207,6 +1208,31 @@ pub fn resolve_free_types(code: &str, prefix: &str, quiet: bool) -> Option<Strin
Some(full_code.strip_prefix(prefix).unwrap().to_string())
}

pub fn resolve_recursive_vars(code: &str, prefix: &str) -> Option<String> {
let full_code = format!("{}{}", prefix, code);
let config = make_config(&full_code);
let suggestions: Vec<_> = run_compiler(config, |compiler| {
compiler.enter(|queries| {
queries.global_ctxt().ok()?.enter(|tcx| {
let mut visitor = RecursiveVarVisitor::new(tcx);
tcx.hir().visit_all_item_likes_in_crate(&mut visitor);
let source_map = tcx.sess.source_map();
let suggestions = visitor
.recursive_vars
.into_iter()
.map(|span| {
let snippet = span_to_snippet(span, source_map);
make_suggestion(snippet, "0")
})
.collect();
Some(suggestions)
})
})
})??;
let full_code = rustfix::apply_suggestions(&full_code, &suggestions).expect(&full_code);
Some(full_code.strip_prefix(prefix).unwrap().to_string())
}

pub fn resolve_imports(code: &str, prefix: &str) -> Option<String> {
let full_code = format!("{}{}", prefix, code);
let config = make_config(&full_code);
Expand Down Expand Up @@ -1796,6 +1822,63 @@ impl<'tcx> Visitor<'tcx> for FreeConstantVisitor<'tcx> {
}
}

struct RecursiveVarVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
items: Vec<Option<LocalDefId>>,
recursive_vars: Vec<Span>,
}

impl<'tcx> RecursiveVarVisitor<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> Self {
Self {
tcx,
items: vec![],
recursive_vars: vec![],
}
}
}

impl<'tcx> RecursiveVarVisitor<'tcx> {
fn handle_expr(&mut self, e: &'tcx Expr<'tcx>) {
let ExprKind::Path(QPath::Resolved(_, path)) = e.kind else { return };
let Res::Def(_, def_id) = path.res else { return };
let Some(def_id) = def_id.as_local() else { return };
let Some(Some(item)) = self.items.last() else { return };
if *item != def_id {
return;
}
if let Node::Expr(parent) = self.tcx.hir().get_parent(e.hir_id) {
if matches!(parent.kind, ExprKind::AddrOf(_, _, _)) {
return;
}
}
self.recursive_vars.push(e.span);
}
}

impl<'tcx> Visitor<'tcx> for RecursiveVarVisitor<'tcx> {
type NestedFilter = nested_filter::OnlyBodies;

fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}

fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
if let ItemKind::Static(_, _, _) | ItemKind::Const(_, _) = i.kind {
self.items.push(Some(i.owner_id.def_id));
} else {
self.items.push(None);
}
intravisit::walk_item(self, i);
self.items.pop();
}

fn visit_expr(&mut self, e: &'tcx Expr<'tcx>) {
self.handle_expr(e);
intravisit::walk_expr(self, e);
}
}

struct ResultVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
suggestions: Vec<Suggestion>,
Expand Down
2 changes: 1 addition & 1 deletion src/llm_client/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Try to avoid unsafe code.",
let m10 = user(&prompt);
let msgs = vec![m1, m2, m3, m4, m5, m6, m7, m8, m9, m10];
let result = self.send_request(msgs, None).await;
extract_name(result)
extract_name(result).replace(' ', "_")
}

async fn translate_variable(&self, code: &str, deps: &[String]) -> Option<String> {
Expand Down
11 changes: 5 additions & 6 deletions src/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,12 +1527,11 @@ impl<'ast> Translator<'ast> {
let translated = compiler::resolve_imports(&translated, &uses.join("")).unwrap();
let item = compiler::parse_one(&translated).unwrap();

let translated = compiler::resolve_free_types(
&item.get_code(),
&prefixes.checking_prefix,
self.config.quiet,
)
.unwrap();
let translated =
compiler::resolve_recursive_vars(&item.get_code(), &prefixes.checking_prefix).unwrap();
let translated =
compiler::resolve_free_types(&translated, &prefixes.checking_prefix, self.config.quiet)
.unwrap();
let item = compiler::parse_one(&translated).unwrap();

let translated =
Expand Down

0 comments on commit 50619d2

Please sign in to comment.