@@ -137,3 +137,94 @@ def test_bound_never_points_at_the_function_itself(tmp_path):
137137 r , nid = _build (tmp_path )
138138 for label in ("sort()" , "program()" , ".member()" ):
139139 assert nid [label ] not in _bound_targets (r , label , nid )
140+
141+
142+ # Class-level context bounds (#4080). `class_definition`, `trait_definition`
143+ # and `enum_definition` carry the same `type_parameters` node as a method, so
144+ # the bound is the same typeclass dependency, attributed to the type itself.
145+ CLASS_SRC = '''\
146+ trait Ordering
147+ trait Show
148+ trait Eq
149+ trait Async
150+ trait Comparable
151+
152+ class Sorted[A: Ordering](xs: List[A])
153+
154+ case class Pretty[T: Show: Eq](t: T)
155+
156+ trait Effectful[F[_]: Async]
157+
158+ enum Ranked[A: Ordering] { case Low }
159+
160+ class Plain[T](t: T)
161+
162+ class Upper[T <: Comparable[T]](t: T)
163+
164+ class Holder {
165+ def member[K: Ordering](k: K): K = k
166+ }
167+ '''
168+
169+
170+ def _build_classes (tmp_path ):
171+ (tmp_path / "ClassContextBounds.scala" ).write_text (CLASS_SRC )
172+ r = extract_scala (tmp_path / "ClassContextBounds.scala" )
173+ nid = {n ["label" ]: n ["id" ] for n in r ["nodes" ]}
174+ return r , nid
175+
176+
177+ def test_class_context_bound_emits_reference (tmp_path ):
178+ r , nid = _build_classes (tmp_path )
179+ assert nid ["Ordering" ] in _bound_targets (r , "Sorted" , nid )
180+
181+
182+ def test_case_class_several_context_bounds_each_emit_a_reference (tmp_path ):
183+ r , nid = _build_classes (tmp_path )
184+ targets = _bound_targets (r , "Pretty" , nid )
185+ assert nid ["Show" ] in targets
186+ assert nid ["Eq" ] in targets
187+
188+
189+ def test_trait_higher_kinded_context_bound_emits_reference (tmp_path ):
190+ r , nid = _build_classes (tmp_path )
191+ assert nid ["Async" ] in _bound_targets (r , "Effectful" , nid )
192+
193+
194+ def test_enum_context_bound_emits_reference (tmp_path ):
195+ r , nid = _build_classes (tmp_path )
196+ assert nid ["Ordering" ] in _bound_targets (r , "Ranked" , nid )
197+
198+
199+ def test_class_without_context_bound_emits_no_bound_edge (tmp_path ):
200+ r , nid = _build_classes (tmp_path )
201+ assert not _bound_targets (r , "Plain" , nid )
202+ assert not _bound_targets (r , "Upper" , nid )
203+
204+
205+ def test_class_context_bound_keeps_constructor_param_as_field (tmp_path ):
206+ # The bound must not displace the class_parameters walk: `xs: List[A]`
207+ # still references List under the `field` context.
208+ r , nid = _build_classes (tmp_path )
209+ refs = {(e ["target" ], e .get ("context" )) for e in r ["edges" ]
210+ if e ["relation" ] == "references" and e ["source" ] == nid ["Sorted" ]}
211+ assert (nid ["List" ], "field" ) in refs
212+ assert (nid ["List" ], "type_bound" ) not in refs
213+
214+
215+ def test_member_bound_is_not_hoisted_to_the_enclosing_class (tmp_path ):
216+ r , nid = _build_classes (tmp_path )
217+ assert nid ["Ordering" ] in _bound_targets (r , ".member()" , nid )
218+ assert not _bound_targets (r , "Holder" , nid )
219+
220+
221+ def test_class_bound_edge_is_tagged_extracted_with_a_location (tmp_path ):
222+ r , nid = _build_classes (tmp_path )
223+ edges = [e for e in r ["edges" ]
224+ if e ["relation" ] == "references"
225+ and e ["source" ] == nid ["Sorted" ]
226+ and e .get ("context" ) == "type_bound" ]
227+ assert edges
228+ for e in edges :
229+ assert e .get ("confidence" ) == "EXTRACTED" , e
230+ assert e .get ("source_location" ) == "L7" , e
0 commit comments